有人知道如何将实时相机输入到UIImageView
吗?
我有一个自定义用户界面,我需要显示相机输入(前置摄像头),因此我无法使用UIImagePickerControl
。
答案 0 :(得分:4)
您需要创建一个捕获会话,并开始运行。完成后,您可以将捕获会话中的图层添加到视图中:
- (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];
}
完成后,您可以按如下方式创建预览图层:
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];
然后将预览图层添加到视图中:
_myView.layer addSubLayer:_previewLayer];