Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAssetImageGenerator provides images rotated

When obtaining a UIImage of a video via AVAssetImageGenerator, I'm getting back images rotated (well, technically they're not) when the video is shot in portrait orientation. How can I tell what orientation the video was shot and then rotate the image properly?

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(0, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
[generate release];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
like image 888
E-Madd Avatar asked Mar 18 '11 03:03

E-Madd


2 Answers

The easiest way is to just set the appliesPreferredTrackTransform property on the image generator to YES, then it should automatically do the transformation for you.

like image 179
Anomie Avatar answered Nov 09 '22 16:11

Anomie


The copy and paste solution to create image with the recording orientation using the previous answer.

AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CGImageRef cgImage = [imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil];
UIImage* image = [UIImage imageWithCGImage:cgImage];

CGImageRelease(cgImage);
like image 25
ant_one Avatar answered Nov 09 '22 18:11

ant_one