I am using AVFoundation to capture still images progmatically with the front camera in the portrait orientation camera view in a UIViewController. This is all good.
The problem I have is when I display the pictures I took in the current session inside the UIImageView through animation all the pictures are being displayed as landscape left, I need the pictures to be displayed in the orientation they were taken in which is portrait.
I have tried forcing the device/image orientation to portrait Up but have not had any luck.
Would be great if someone can tell me where I am going wrong. Thank you
I have included my code snippets below.
//Take picture and store images taken to mutable array
-(void)takePicture {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
NSLog(@"attachements: %@", exifAttachments);
}
else
NSLog(@"no attachments");
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: image.CGImage scale:1.0 orientation:UIImageOrientationUp];
NSLog(@"the image oreintation is %i", orientedImage.imageOrientation);
[photosTakenArray addObject:orientedImage];
...
This is how I am displaying the images the camera took inside another UIViewController using the UIImageView startAnimating function. This is where issue is of the pictures taken by camera all being displayed landscape left. photoViewer is my UIImageView in this case and photosToShow is an NSArray reference to the photosTakenArray above.
if ([photosToShow count]>0) {
NSLog(@"we have %i photos to display", [photosToShow count]);
photoViewer.animationImages = photosToShow;
// all frames will execute in 1.75 seconds
photoViewer.animationDuration = 1.75;
// repeat the annimation forever
photoViewer.animationRepeatCount = 0;
// start animating
[photoViewer startAnimating];
...