如果AVCaptureVideoPreviewLayer
方向正确,您可以在捕获图像之前简单地设置方向。
AVCaptureStillImageOutput *stillImageOutput;
AVCaptureVideoPreviewLayer *previewLayer;
NSData *capturedImageData;
AVCaptureConnection *videoConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if ([videoConnection isVideoOrientationSupported]) {
[videoConnection setVideoOrientation:previewLayer.connection.videoOrientation];
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments =
CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
// Do something with the attachments.
}
// TODO need to manually add GPS data to the image captured
capturedImageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [UIImage imageWithData:capturedImageData];
}];
此外,重要的是要注意这一点,UIImageOrientation
并且AVCaptureVideoOrientation
是不同的。UIImageOrientationUp
是指横向模式,音量控件向下朝向地面(如果您考虑将音量控件用作快门按钮,则不是向上)。
因此,电源按钮指向天空 ( AVCaptureVideoOrientationPortrait
) 的纵向实际上是UIImageOrientationLeft
。