方法一 :AVFoundation
1 #import <AVFoundation/AVFoundation.h> 2 3 - (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time { 4 AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 5 NSParameterAssert(asset); 6 AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset]; 7 assetImageGenerator.appliesPreferredTrackTransform = YES; 8 assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels; 9 10 CGImageRef thumbnailImageRef = NULL; 11 CFTimeInterval thumbnailImageTime = time; 12 NSError *thumbnailImageGenerationError = nil; 13 thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError]; 14 15 if(!thumbnailImageRef) 16 CXIMLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError); 17 18 UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] : nil; 19 return thumbnailImage; 20 }
方法二:MPMoviePlayerController
1 #import <MediaPlayer/MediaPlayer.h> 2 3 MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 4 moviePlayer.shouldAutoplay = NO; 5 UIImage *thumbnailImage = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];