ios 带自定义视图的相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7639188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:43:49  来源:igfitidea点击:

Camera with Custom View

iosobjective-ccamerauiimagepickercontrollercustom-view

提问by cyclingIsBetter

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.

我的应用程序使用相机,我想在相机预览上添加叠加层。比如我想在使用Camera的时候使用相框,还想添加一个自定义的camera操作栏。请帮我做同样的事情。

回答by Dip Dhingani

You might be trying using UIImagePickerController. But I know this one solution to your problem. You can do it easily using AVCamCaptureManager and AVCamRecorder classes. Apple has a demo program build on its developer site here. It is named 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. It calls the same classes which are called by UIImagePickerController. So your camera will open and start taking input.

您可能正在尝试使用 UIImagePickerController。但我知道这是解决您问题的一种方法。您可以使用 AVCamCaptureManager 和 AVCamRecorder 类轻松完成。苹果在其开发者网站一个演示程序构建这里。它被命名为 AVCam。简单来说,它所做的就是当你点击打开相机时,它会调用负责打开 iPhone 相机并录制视频或捕获音频的类和方法。它调用由 UIImagePickerController 调用的相同类。所以你的相机将打开并开始输入。

Now, if you open the xib file of that AVCam project, you'll find a small UIView object. This view is responsible for displaying the camera's 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.

现在,如果你打开那个 AVCam 项目的 xib 文件,你会发现一个小的 UIView 对象。此视图负责显示相机的提要。您可以根据所需的大小调整该视图的大小,并且相机的输入将显示在该区域中。您还可以根据自己的选择在其周围放置框架图像。

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.

当我想调整相机输入源的大小并拍摄照片时,它对我有用。我希望它也适用于你。

回答by Oliver

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];

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

OverlayViewController是您必须编写的控制器来控制您添加到叠加层上的所有内容。

pickerReferenceis 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];

回答by AliSoftware

Read the UIImagePickerController Class Reference, that's right in the documentation…

阅读UIImagePickerController Class Reference,就在文档中......

There are properties for this, especially the cameraOverlayViewand showsCameraControlsproperties.

这有一些属性,尤其是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.

因此,您可以隐藏控件,提供自定义叠加视图,并将子视图添加到此自定义视图以添加自定义按钮、框架等。

回答by NikeAlive

Swift 3 version for answer from Oliver:

来自 Oliver 的 Swift 3 版本:

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