UIImageView中的实时相机 [英] Live camera in UIImageView
本文介绍了UIImageView中的实时相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何将实时相机输入到 UIImageView
吗?
Does anybody know how I can get a live camera feed into an UIImageView
?
我有自定义UI我需要显示相机进纸(前置摄像头)所以我不能使用 UIImagePickerControl
。
I have a custom UI where I need to show the camera feed (front facing camera) so I cannot use the UIImagePickerControl
.
推荐答案
您需要创建一个捕获会话,并开始运行。完成后,您可以将捕获会话中的图层添加到视图中:
You need to create a capture session, and start it running. Once that's done you can add the layer from the capture session to your view:
- (void)setupCaptureSession
{
NSError* error = nil;
// Create the session
_captureSession = [[AVCaptureSession alloc] init];
_captureSession.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[_captureSession addInput:input];
AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
[_captureSession addOutput:output];
// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myCameraOutputQueue", NULL);
//If you want to sebsequently use the data, then implement the delegate.
[output setSampleBufferDelegate:self queue:queue];
}
完成后,您可以按如下方式创建预览图层:
Having done that, you can create a preview layer as follows:
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];
然后将预览图层添加到视图中:
And then add the preview layer to your view:
_myView.layer addSubLayer:_previewLayer];
这篇关于UIImageView中的实时相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文