嗯,这种使用 AVAssetImageGenerator 的方式有效:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CGFloat videoLenght=((float)asset.duration.value)/((float)asset.duration.timescale);
CMTime thumbTime = CMTimeMakeWithSeconds(videoLenght,2.0);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}else{
UIImage *image=[self.class scaledImage:[UIImage imageWithCGImage:im]];
[self performSelectorOnMainThread:@selector(setVideoImage:) withObject:image waitUntilDone:NO];
}
};
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
(videoLenght,2.0) 是快照发生的时间,在这种情况下是视频的一半,我不建议您使用固定的截图时间,因为如果时间超出范围,图像将为零。