First frame of a video using AVFoundation

后端 未结 3 525
庸人自扰
庸人自扰 2021-02-01 19:56

I\'m trying to get the first frame of a video using the classes in AVFoundation. But it appears to not be getting an image at all.

My code currently looks like this

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 20:27

    I used null0pointer solution but in some videos I recieved the first frame rotated. To resolve this issue I set to TRUE the appliesPreferredTrackTransform property of AVAssetImageGenerator. And this code looks like this:

    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
    AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    [imageGenerator setAppliesPreferredTrackTransform:TRUE];
    UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
    [self.imageView setImage:image];
    

提交回复
热议问题