从库中选取的视频的 UIImagePickerController 缩略图
我正在尝试使用 UIImagePickerController 获取从库中拾取的视频的缩略图。
这是我的代码。如果我使用相机录制视频,我可以获得视频的缩略图。但是,如果我从库中选取视频,则无法获取缩略图。有人知道为什么吗?
-(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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了一个半相关的问题,最终放弃使用 MPMoviePlayer 生成缩略图。尝试使用 此处。这是我自己的示例代码,它获取单个缩略图。您需要包含 AVFoundation 框架。
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.
AssetsLibrary 框架正是您所需要的使用
ALAssetsLibrary
和ALAsset
类。此代码适用于照片和视频,缩略图与您在选择器中看到的完全相同。请注意,iOS 8 上的照片流相册存在错误,请参阅 ALAssetsLibrary assetForURL:对于 iOS 8.1 中“我的照片流”中的照片始终返回 nil 作为解决方法。
The AssetsLibrary framework has exactly what you need with the
ALAssetsLibrary
andALAsset
classes. This code work for both photos and videos and the thumbnail image is exactly the same as you see in the picker.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.
Swift 5 及更高版本
Swift 5 and above
Swift 3.1
我创建了一个类函数并在参数中传递你的 url
Swift 3.1
I have create a class function and pass your url in param