如何创建自定义相机视图,而不是UIImagePickerViewController?

时间:2022-09-14 09:28:28

I created a simple application using UIImagePickerViewController for capturing from the camera, but I'd like to customize the interface for grabbing from the camera, such as adding some buttons.

我使用UIImagePickerViewController创建了一个简单的应用程序,用于从相机捕获,但我想自定义从相机抓取的界面,例如添加一些按钮。

UIImagePickerViewController doesn't allow this directly, so how would you create a custom view that allows for displaying the live camera feed and capturing from it?

UIImagePickerViewController不允许直接使用它,那么如何创建一个自定义视图,允许显示实时摄像头源并从中捕获?

3 个解决方案

#1


30  

The easy way is to continue to use UIImagePickerViewController but set showsCameraControls to NO and provide your own user interface using cameraOverlayView.

简单的方法是继续使用UIImagePickerViewController但将showsCameraControls设置为NO并使用cameraOverlayView提供您自己的用户界面。

The more difficult (but more flexible) way is to use the AVFoundation classes (particularly AVCaptureVideoPreviewLayer and AVCaptureStillImageOutput) to construct your own camera.

更困难(但更灵活)的方法是使用AVFoundation类(特别是AVCaptureVideoPreviewLayer和AVCaptureStillImageOutput)来构建自己的相机。

#2


23  

Yes, create a UIImagePickerController from code, adjust its properties, add an overlay onto it, and with you controller, control whatever you want on that overlay : custom controls, overlaying images, etc...

是的,从代码创建UIImagePickerController,调整其属性,在其上添加叠加,并与您的控制器,控制在该叠加上你想要的任何内容:自定义控件,覆盖图像等...

That gives something like this :

这给出了这样的东西:

self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;

// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:@"Overlay" bundle:nil];
self.overlay.pickerReference = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;

[self presentModalViewController:self.picker animated:NO];

OverlayViewController is the controller that you must write to control everything you add onto the overlay.

OverlayViewController是您必须编写的控制器,用于控制添加到叠加层中的所有内容。

pickerReference is a property you can keep to send orders to the camera. For example, you could call the following from an IBAction coming from a UIButton placed onto the overlay :

pickerReference是您可以保留向相机发送订单的属性。例如,您可以从放置在叠加层上的UIButton的IBAction中调用以下内容:

[self.pickerReference takePicture];

#3


4  

You need to set the value of showCameraControls to NO and provide your own custom overlay view using the cameraOverlayView.

您需要将showCameraControls的值设置为NO,并使用cameraOverlayView提供您自己的自定义叠加视图。

#1


30  

The easy way is to continue to use UIImagePickerViewController but set showsCameraControls to NO and provide your own user interface using cameraOverlayView.

简单的方法是继续使用UIImagePickerViewController但将showsCameraControls设置为NO并使用cameraOverlayView提供您自己的用户界面。

The more difficult (but more flexible) way is to use the AVFoundation classes (particularly AVCaptureVideoPreviewLayer and AVCaptureStillImageOutput) to construct your own camera.

更困难(但更灵活)的方法是使用AVFoundation类(特别是AVCaptureVideoPreviewLayer和AVCaptureStillImageOutput)来构建自己的相机。

#2


23  

Yes, create a UIImagePickerController from code, adjust its properties, add an overlay onto it, and with you controller, control whatever you want on that overlay : custom controls, overlaying images, etc...

是的,从代码创建UIImagePickerController,调整其属性,在其上添加叠加,并与您的控制器,控制在该叠加上你想要的任何内容:自定义控件,覆盖图像等...

That gives something like this :

这给出了这样的东西:

self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;

// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:@"Overlay" bundle:nil];
self.overlay.pickerReference = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;

[self presentModalViewController:self.picker animated:NO];

OverlayViewController is the controller that you must write to control everything you add onto the overlay.

OverlayViewController是您必须编写的控制器,用于控制添加到叠加层中的所有内容。

pickerReference is a property you can keep to send orders to the camera. For example, you could call the following from an IBAction coming from a UIButton placed onto the overlay :

pickerReference是您可以保留向相机发送订单的属性。例如,您可以从放置在叠加层上的UIButton的IBAction中调用以下内容:

[self.pickerReference takePicture];

#3


4  

You need to set the value of showCameraControls to NO and provide your own custom overlay view using the cameraOverlayView.

您需要将showCameraControls的值设置为NO,并使用cameraOverlayView提供您自己的自定义叠加视图。