有人知道我如何将实时摄像头输入UIImageView吗?

我有一个自定义UI,需要在其中显示摄像头供稿(前置摄像头),因此无法使用UIImagePickerControl

最佳答案

您需要创建一个捕获 session ,然后开始运行。完成后,您可以将捕获 session 中的图层添加到视图中:

- (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];

关于iphone - UIImageView中的实时摄像头,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14337489/

10-15 15:54