ios – 使用cameraOverlayView与UIImagePickerController

前端之家收集整理的这篇文章主要介绍了ios – 使用cameraOverlayView与UIImagePickerController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这已经被问了一些时间,但我找不到答案.

我有一个应用程序使用UIImagePickerController来拍照.问题是我只想要相机选项可用,我明白我需要隐藏标准控件:

cameraUI.showsCameraControls=NO;

并使用cameraOverlayView来提供我自己的控件.我已经看过苹果的PhotoPicker项目,我的初始问题是如何将Overlay对象放到我的故事板上?我在图书馆里找不到这样的对象.任何帮助感激不尽.

解决方法

这是代码
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)],nil];
[toolBar setItems:items];

// create the overlay view
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0,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;

从ApplePhotoPicker添加此OverlayView.h和.m类.

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

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

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

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

快乐编码:)

原文链接:https://www.f2er.com/iOS/328652.html

猜你在找的iOS相关文章