我正在向用户显示预览,并在3秒后拍摄图像。
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
[session stopRunning];
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
}];
因为captureStillImageAsynchronouslyFromConnection需要一些时间,如果我在3秒之前移动手机,拍摄的照片将与预览中显示的相同(AVCaptureVideoPreviewLayer)。
我可以避免这个吗?
答案 0 :(得分:0)
您应该尝试冻结/渲染预览图层,而不是停止AVCaptureSession。
您可以尝试使用此代码从CALayer创建UIImage,只在预览图层上的UIImageView中显示图像。我不知道这种渲染是否足够快,但值得一试。
- (UIImage *)imageFromLayer:(CALayer *)layer
{
UIGraphicsBeginImageContext([layer frame].size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
此代码取自: