简体   繁体   English

相机与自定义视图

[英]Camera with Custom View

My Application uses camera, I would like to add overlay over the camera preview. 我的应用程序使用相机,我想在相机预览中添加叠加。 For example, I want to use a picture frame when I use Camera, also I would like to add a custom bar for camera operations. 例如,我想在使用相机时使用相框,我也想为相机操作添加自定义条。 Kindly help me to do the same. 请帮我做同样的事。

You might be trying using UIImagePickerController. 您可能正在尝试使用UIImagePickerController。 But I know this one solution to your problem. 但我知道这个问题的解决方案。 You can do it easily using AVCamCaptureManager and AVCamRecorder classes. 您可以使用AVCamCaptureManager和AVCamRecorder类轻松完成。 Apple has a demo program build on its developer site here . 苹果在其开发者网站一个演示程序构建这里 It is named AVCam. 它被命名为AVCam。 In simple words what it does is when you click to open the camera, it calls the classes and methods which are responsible for opening the iPhone's camera and record video or capture audio. 简单来说,当您单击打开相机时,它会调用负责打开iPhone相机并录制视频或捕获音频的类和方法。 It calls the same classes which are called by UIImagePickerController. 它调用UIImagePickerController调用的相同类。 So your camera will open and start taking input. 所以你的相机会打开并开始输入。

Now, if you open the xib file of that AVCam project, you'll find a small UIView object. 现在,如果您打开该AVCam项目的xib文件,您将找到一个小的UIView对象。 This view is responsible for displaying the camera's feed. 此视图负责显示相机的Feed。 You can resize that view as per the size you want and the camera's input will be displayed in that much area. 您可以根据需要调整视图大小,摄像机的输入将显示在该区域中。 You can also put the frame image around it as per your choice. 您也可以根据自己的选择将框架图像放在其周围。

It worked for me when I wanted to resize the camera's input feed and capture photos. 当我想调整相机的输入源并拍摄照片时,它对我有用。 I hope it works for you as well. 我希望它对你也有用。

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. pickerReference是您可以保留向相机发送订单的属性。 For example, you could call the following from an IBAction coming from a UIButton placed onto the overlay : 例如,您可以从放置在叠加层上的UIButton的IBAction中调用以下内容:

[self.pickerReference takePicture];

Read the UIImagePickerController Class Reference , that's right in the documentation… 阅读UIImagePickerController类参考 ,这在文档中是正确的...

There are properties for this, especially the cameraOverlayView and showsCameraControls properties. 有一些属性,尤其是cameraOverlayViewshowsCameraControls属性。

So you can hide the controls, provide a custom overlay view, and add subviews to this custom view to add custom buttons, frames, etc. 因此,您可以隐藏控件,提供自定义叠加视图,并将子视图添加到此自定义视图以添加自定义按钮,框架等。

Swift 3 version for answer from Oliver: Swift 3版本的答案来自Oliver:

self.picker = UIImagePickerController()
self.picker.sourceType = .camera
self.picker.cameraCaptureMode = .photo
self.picker.cameraDevice = .rear
self.picker.showsCameraControls = false
self.picker.isNavigationBarHidden = true
self.picker.isToolbarHidden = true

// Insert the overlay
self.overlayViewController = self.storyboard?.instantiateViewController(withIdentifier: "Overlay") as! OverlayViewController

self.picker.cameraOverlayView = self.overlayViewController.view
self.picker.delegate = self.overlayViewController
self.navigationController?.present(self.picker, animated: true, completion: nil)

OverlayViewController protocols: OverlayViewController协议:

class OverlayViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM