iosiphonevideoavassetavassetimagegenerator

Get orientation from video or thumb obtained with AVAssetImageGenerator


In my application I create videos from single images. Everything works fine, videos are assembled correctly with right size and orientation. They are displayed correctly both in the Apple photo app, from MPMoviePlayer and from the sandbox directory where I save them.
The problem arise when I try to get a thumb from a movie. The orientation is not correct and I don't know how to fix, I've seen that there is a -preferredTransform property but the result is the the same for both landscape and portrait videos.
The url I'm using is the sandbox directory path. Here is the snippet:

- (void) setVideoPath:(NSString *)videoPath {
    if (videoPath ==_videoPath) {
        return;
    }
    _videoPath = videoPath;
    AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:_videoPath]];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(1, 1);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);  
    self.videoImageView.image = thumbnail;
}

Solution

  • Just use this, No need to write any methods

    generator.appliesPreferredTrackTransform=true