我正在以这种方式创建会话和 AVCaptureStillImageOutput :
__imageCaptureSession = [[AVCaptureSession alloc] init];
__imageCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;
__imageCapture = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[__imageCapture setOutputSettings:outputSettings];
[__imageCaptureSession addInput:input];
[__imageCaptureSession addOutput:__imageCapture];
输入是后置摄像头。要拍照,我使用下面的代码。问题是,拍摄照片需要花费太多时间。有可能解决这个问题吗?
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in __imageCapture.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
[__imageCapture captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
//metadata
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(NSDictionary*)exifAttachments];
//image
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
//send notification
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[image retain] ,kImageKey,
[metadata retain], kMetadataKey,
nil];
[metadata release];
[[NSNotificationCenter defaultCenter]postNotificationName:kTakePhotoNotification object:nil userInfo:dict];
[__captureSession stopRunning];
[image release];
}];