UIImagePickerController thumbnail of video which is pick up from library
I am trying to get the thumbnail of the video which is pick up from library using the UIImagePickerController.
Here is my code. I can get the the thumbnail开发者_JS百科 of video if I using the camera to record a video. But I can not get the thumbnail if I pick up a video from the library. Anybody know why?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [[moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame] retain];
[imageView setImage:thumbnail]; //imageView is a UIImageView
[moviePlayer release];
[thumbnail release];
[self dismissModalViewControllerAnimated:YES];
}
}
I had a semi-related problem, and eventually abandoned using MPMoviePlayer to generate thumbnails. Try using AVAssetImageGenerator instead. Apple discusses using AVAssetImageGenerator to create thumbnails here. Here is my own sample code, which grabs a single thumbnail image. You will need to include the AVFoundation framework.
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
[gen release];
The AssetsLibrary framework has exactly what you need with the ALAssetsLibrary
and ALAsset
classes. This code work for both photos and videos and the thumbnail image is exactly the same as you see in the picker.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[ALAssetsLibrary new] assetForURL:info[UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
imageView.image = [UIImage imageWithCGImage:asset.thumbnail];
} failureBlock:^(NSError *error) {
// handle error
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
Note that there is a bug on iOS 8 with Photo Stream albums, see ALAssetsLibrary assetForURL: always returning nil for photos in “My Photo Stream” in iOS 8.1 for a workaround.
func videoSnapshot(filePathLocal:URL) -> UIImage? {
do
{
let asset = AVURLAsset(url: filePathLocal)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(0), Int32(1)),actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
}
catch let error as NSError
{
print("Error generating thumbnail: \(error)")
return nil
}
}
Swift 5 and above
func thumbnailForVideo(url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let assetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
time.value = min(time.value, 2)
do {
let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
return UIImage(cgImage: imageRef)
} catch {
print("failed to create thumbnail")
return nil
}
}
Swift 3.1
I have create a class function and pass your url in param
class func captureThumbnail(withVideoURL videoURL:URL,secs:Int,preferredTimeScale scale:Int,completionHandler:((UIImage?) ->Void)?) -> Void
{
//let seconds : Int64 = 10
// let preferredTimeScale : Int32 = 1
DispatchQueue.global().async {
do
{
let asset = AVURLAsset(url: videoURL)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at:CMTimeMake(Int64(secs), Int32(scale)),actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
completionHandler?(thumbnail)
}
catch let error as NSError
{
print("Error generating thumbnail: \(error)")
completionHandler?(nil)
}
}
}
精彩评论