更简单的子视图UIImagePickerController的方法?地雷没有用

时间:2013-09-03 03:03:03

标签: ios uikit uiimagepickercontroller

当我运行我的代码并使用下面的代码启动相机时,我得到我的自定义overlayView以及默认的相机按钮。唯一的问题是,现在,我得到一个空白的黑盒子,我的UIImagePicker曾经是。我只是尝试使用默认的UIImagePicker控件和按钮创建一个摄像头,然后在默认的imagePickerController顶部添加我自己的IBOutlet UILabel,以便我的标签与所有默认的摄像头控件一起显示。我虽然很难完成这件事。干杯!

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.image == nil &&  [self.videoFilePath length] == 0) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = NO;
        self.imagePicker.videoMaximumDuration = 10;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

        [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
        self.overlayView.frame = self.imagePicker.cameraOverlayView.frame;
        self.imagePicker.cameraOverlayView = self.overlayView;
        self.overlayView = nil;
    }
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:0)

尝试使用我的代码。它工作正常。可能是你实现了你想要的。我创建了一个控制器,并使用它的视图来设置cameraOverlayView属性。我没有使用nib文件,我以编程方式创建了所有内容。

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.tabBarItem.image = [UIImage imageNamed:@"86-camera"];

self.cameraOverlayViewController = [[CameraOverlayViewController alloc] init];
self.cameraOverlayViewController.picker = picker;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.showsCameraControls = NO;
    picker.toolbarHidden = YES;
    picker.cameraOverlayView = self.cameraOverlayViewController.view;
    picker.delegate = self.cameraOverlayViewController;

}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}

这是自定义CameraOverlayViewController的代码: 接口文件:

#import <UIKit/UIKit.h>

@interface CameraOverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *picker;

@end

实施档案:

#import "CameraOverlayViewController.h"
#import "BButton.h"
#import "AnalyseViewController.h"
#import "Tree.h"

@interface CameraOverlayViewController ()
{
}
@end

@implementation CameraOverlayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.view.backgroundColor = [UIColor clearColor];
        self.view.opaque = NO;

    }
    return self;
}


- (void)loadView
{
    [super loadView];

    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bottom - 150, 320, 150)];
    bottomView.backgroundColor = [UIColor blackColor];
    BButton *button = [[BButton alloc] initWithFrame:CGRectMake(50, 25, 220, 50) type:BButtonTypePrimary];
    [button setTitle:@"Сфотографировать" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:button];
    [self.view addSubview:bottomView];

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
    topView.backgroundColor = [UIColor clearColor];
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 310, 60)];
    infoLabel.backgroundColor = [UIColor clearColor];
    infoLabel.font = [UIFont fontWithName:@"Georgia" size:18];
    infoLabel.textColor = [UIColor whiteColor];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.numberOfLines = 0;
    infoLabel.text = @"Поместите лист дерева внутри рамки, чтобы\nсфотографировать его.";
    [topView addSubview:infoLabel];

    [self.view addSubview:topView];

    UIView *bordersView = [[UIView alloc] initWithFrame:CGRectMake(10, topView.bottom + 10, 300, 300)];
    bordersView.layer.cornerRadius = 10;
    bordersView.layer.borderColor = [UIColor whiteColor].CGColor;
    bordersView.layer.borderWidth = 2;
    bordersView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:bordersView];
}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

    AnalyseViewController *analyseViewController = [[AnalyseViewController alloc] initWithNibName:nil bundle:nil];
    analyseViewController.baseImage = image;
    analyseViewController.treesArray = [Tree retrieveAllTrees];

    UINavigationController *analyseNavController = [[UINavigationController alloc] initWithRootViewController:analyseViewController];

    [AppDel.tabBarController presentViewController:analyseNavController animated:YES completion:nil];
}


#pragma mark - Other methods

- (void)buttonClicked:(id)sender
{
    [self.picker takePicture];
}

@end

自己删除不需要的类,比如BButton。

答案 1 :(得分:0)

用它修复它。我对CGPoints&amp; amp;做了一些研究。 CGRect并提出这个解决方案。不确定它是否是最优雅的解决方案,但它的工作原理似乎很有道理。 - (void)viewDidLoad     {         [super viewDidLoad];

    if (self.image == nil &&  [self.videoFilePath length] == 0) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = YES;
        self.imagePicker.videoMaximumDuration = 10;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
            }
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    self.overlayView.frame = CGRectMake(160,0,0,0);
    self.imagePicker.cameraOverlayView = self.overlayView;

}