If the AVCaptureVideoPreviewLayer
orientation is correct, you can simply set the orientation before you capture the image.
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];
}];
Also, it's important to note that UIImageOrientation
and AVCaptureVideoOrientation
are different. UIImageOrientationUp
refers to landscape mode with the volume controls down toward the ground (not up if you think about using the volume controls as a shutter button).
Thus, portrait orientation with the power button pointing to the sky (AVCaptureVideoOrientationPortrait
) is actually UIImageOrientationLeft
.