在IBAction中调用captureOutput

时间:2015-12-16 11:52:22

标签: ios objective-c uiimageview avfoundation avcaptureoutput

我想创建一个应用,点击IBAction按钮后,应在UIImageView上显示来自实时相机的当前帧。 使用此方法:https://developer.apple.com/library/ios/qa/qa1702/_index.html
我想在IBAction功能中创建功能。这一个确切地说:

- (void)captureOutput:(AVCaptureOutput *)captureOutput

不幸的是,经过十几次尝试,我无法做到。

  

总结:如何创建一个包含图像的函数(void)   UIImage形成并将其保存到故事板中的对象?

我会感激任何帮助。 问候。

1 个答案:

答案 0 :(得分:0)

使用此方法捕获来自AVCaptureSession stillImageOutput AVCaptureStillImageOutput的{​​{1}}的静止图像:

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
    CVImageBufferRef imageBuffer =CMSampleBufferGetImageBuffer(imageSampleBuffer);
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage =[temporaryContext
        createCGImage:ciImage
        fromRect:CGRectMake(
            0, 0,
            CVPixelBufferGetWidth(imageBuffer),
            CVPixelBufferGetHeight(imageBuffer)
        )];
    UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
    self.imageView.image = image;
}