cor*_*erg 0 camera cocoa-touch overlay uiimagepickercontroller ios
我正在创建一个应用程序,让用户可以在"镜像"中看到自己(设备上的前置摄像头).我知道使用视图叠加制作UIImageViewController的多种方法,但我希望我的应用程序具有相反的方式.在我的应用程序中,我希望摄像机视图是主视图的子视图,没有快门动画或捕获照片或拍摄视频的能力,而不是全屏.有任何想法吗?
Bil*_*son 15
实现此目的的最佳方法是不使用内置的UIImagePickerController,而是使用AVFoundation
类.
您想要创建AVCaptureSession
并设置适当的输出和输入.配置完成后,您可以将AVCapturePreviewLayer
其添加到视图控制器中已配置的视图中.预览图层具有许多属性,可用于控制预览的显示方式.
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];
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用captureStillImageAsynchronouslyFromConnection:completionHandler:
输出设备捕获图像.
有关如何构建AVFoundation的更多信息以及如何更详细地执行此操作的示例,请查看Apple Docs.Apple的AVCamDemo也完成了这一切