ios - 将 cameraOverlayView 与 UIImagePickerController 结合使用

标签 ios objective-c uiimagepickercontroller camera-overlay

我有一个使用 UIImagePickerController 拍照的应用程序。问题是我只希望相机选项可用,而且我知道我需要隐藏标准控件:

cameraUI.showsCameraControls=NO;

并使用 cameraOverlayView 提供我自己的控件。我已经看过 Apple 的 PhotoPicker 项目,我最初的问题是如何将 Overlay 对象放到 Storyboard上?我在图书馆找不到这样的对象。

最佳答案

代码如下:

toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-54, self.view.frame.size.width, 55)];

toolBar.barStyle =  UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel  target:self action:@selector(cancelPicture)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera  target:self action:@selector(shootPicture)],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil],
                nil];
[toolBar setItems:items];

// create the overlay view
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];

// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];

// parent view for our overlay
UIView *cameraView=[[UIView alloc] initWithFrame:self.view.bounds];
[cameraView addSubview:overlayView];
[cameraView addSubview:toolBar];

imagePickerController = [[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){
    NSLog(@"Camera not available");
    return;
}
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;

// hide the camera controls
imagePickerController.showsCameraControls=NO;
imagePickerController.wantsFullScreenLayout = YES;
[imagePickerController setCameraOverlayView:cameraView];

[self presentViewController:imagePickerController animated:YES completion:nil];

在头文件中声明:

UIImagePickerController * imagePickerController;
UIToolbar *toolBar;
OverlayView *overlayView;

从 Apples PhotoPicker 添加这个 OverlayView.h 和 .m 类。

使用自定义相机按钮拍摄照片的操作:

-(void) shootPicture {
    [imagePickerController takePicture];
}

- (IBAction)cancelPicture {
    [self dismissViewControllerAnimated:YES completion:nil];
}

输出将如下图所示(我在自定义叠加 View 中添加了捕获按钮和取消按钮):

enter image description here

快乐编码:)

关于ios - 将 cameraOverlayView 与 UIImagePickerController 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590346/

相关文章:

objective-c - 在main()中使用NSURLConnection时,为什么连接无法完成?

objective-c - 如何解析 obj-c 中的动态 json 数据?

ios - UIImagePickerController 在关闭时复制内容

ios - Google map iOS 当前位置默认值

ios - 在我的一个 iOS 应用程序 (Swift) 中需要一个时间对象

ios - 获取当前行的正确编号(iOS)

ios - 适用于 iOS 的自定义图像选择器

ios - 使用 UIImagePickerController 减少内存使用

ios - displayModeButtonItem 实际上做了什么?

ios - iBeacon 案例 - 触发具有距离限制的 didEnterRegion