我想要最后一帧视频,我的代码如下所示:
let asset : AVURLAsset = AVURLAsset(URL: videoURL,options: nil) let generate : AVAssetimageGenerator = AVAssetimageGenerator(asset: asset) generate.appliesPreferredTrackTransform = true var err : NSError? = nil var lastFrameTime = Int64(CMTimeGetSeconds(asset.duration)) let time : CMTime = CMTimeMake(lastFrameTime,30) let imgRef : CGImageRef = generate.copyCGImageAtTime(time,actualTime: nil,error: &err) let img : UIImage = UIImage(CGImage: imgRef)!
但我没有得到最后一帧,我得到第一帧,为我提供解决方案.
解决方法
你可以在Objective-C中获得最后一帧视频,如下所述: –
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; AVAssetimageGenerator *generate = [AVAssetimageGenerator assetimageGeneratorWithAsset:asset]; generate.appliesPreferredTrackTransform = YES; NSError *error; int64_t lastFrameTime = CMTimeGetSeconds(asset.duration)*60.0; CMTime time = CMTimeMake(lastFrameTime,2); CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:nil error:&error]; UIImage *image = [UIImage imageWithCGImage:imgRef];