如何查看相机视图? [英] How to subview a camera view?

查看:211
本文介绍了如何查看相机视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,让用户在镜像(设备上的前置摄像头)看到自己。我知道用一个视图覆盖制作一个UIImageViewController的多种方式,但我想我的应用程序有相反的方式。在我的应用程序中,我想让相机视图成为主视图的子视图,没有快门动画或捕捉照片或拍摄视频,而不是全屏的能力。任何想法?

I am making an app that will let the user see themselves in a 'mirror' (the front facing camera on the device). I know of multiple ways of making a UIImageViewController with a view overlay, but I want my app to have it be the opposite way. In my app, I want the camera view to be a subview of the main view, without the shutter animation or the ability to capture photos or take videos and without it being full screen. Any ideas?

推荐答案

实现这个的最好方法是不使用内置的UIImagePickerController, c $ c> AVFoundation 类。

The best way to accomplish this is to not use the built-in UIImagePickerController, but rather use the AVFoundation classes.

您要创建 AVCaptureSession 并设置适当的输出和输入。配置完成后,您可以得到一个 AVCapturePreviewLayer ,它可以添加到您在视图控制器中配置的视图。预览图层具有许多属性,允许您控制预览的显示方式。

You want to create an AVCaptureSession and set the appropriate outputs and inputs. Once it's configured you can get an AVCapturePreviewLayer which can be added to a view that you have configured in your view controller. The preview layer has a number of properties that allow you to control how the preview is displayed.

AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureOutput *output = [[AVCaptureStillImageOutput alloc] init];
[session addOutput:output];

//Setup camera input
NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
//You could check for front or back camera here, but for simplicity just grab the first device
AVCaptureDevice *device = [possibleDevices objectAtIndex:0];
NSError *error = nil;
// create an input and add it to the session
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; //Handle errors

//set the session preset 
session.sessionPreset = AVCaptureSessionPresetMedium; //Or other preset supported by the input device   
[session addInput:input];

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
//Set the preview layer frame
previewLayer.frame = self.cameraView.bounds;
//Now you can add this layer to a view of your view controller
[self.cameraView.layer addSublayer:previewLayer]
[session startRunning];

然后可以使用 captureStillImageAsynchronouslyFromConnection:completionHandler:以捕获图像。

You can then use the captureStillImageAsynchronouslyFromConnection:completionHandler: of the output device to capture an image.

有关如何结构化AVFoundation的更多信息,以及如何执行此操作的示例,请参阅 Apple文档
Apple的 AVCamDemo 也能够显示所有这些

For more information on how AVFoundation is structured and examples on how to do this in more detail checkout the Apple Docs. Apple's AVCamDemo lays all of this out as well

这篇关于如何查看相机视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆