UIImageView中的实时相机
有谁知道我可以得到一个实时摄像头饲料到UIImageView
?
我有一个自定义用户界面,我需要显示摄像头源(前置摄像头),所以我不能使用UIImagePickerControl
。
您需要创build一个捕获会话,并开始运行。 一旦完成,您可以将捕获会话中的图层添加到您的视图中:
- (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]; }
完成之后,您可以创build一个预览图层,如下所示:
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession]; [_captureSession startRunning];
然后将预览图层添加到您的视图中:
_myView.layer addSubLayer:_previewLayer];