gpt4 book ai didi

ios - 将 cameraOverlayView 与 UIImagePickerController 结合使用

转载 作者:技术小花猫 更新时间:2023-10-29 11:00:49 36 4
gpt4 key购买 nike

我有一个使用 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com