我用这个作为参考:从视频 URL 或 iPhone SDK 中的数据获取缩略图 https://stackoverflow.com/a/6027285/1145339
该方法使用 MPMoviePlayerController 类而不是 AVFoundation,我想我也想使用它,因为人们说 MPMoviePlayer 方式比 AVFoundation 方式更快。
问题是,用于创建缩略图的方法,[player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]
在 iOS 7.0 中已弃用。
通过查看苹果文档,创建缩略图的其余支持方法是通过以下方法(void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option
and (void)cancelAllThumbnailImageRequests
。但是,正如方法签名所规定的那样,这些方法不返回任何内容。那么我如何访问UIImage
通过这些方法创建的缩略图?
如果有帮助,这就是我到目前为止的代码:
self.videoURL = info[UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
//Create thumbnail image
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
[player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame];
//UIImage *thumbnail = ???
如何获取缩略图的 UIImage 引用?
EDIT我想出了如何为缩略图请求创建通知(使用这个问题 https://stackoverflow.com/questions/14297726/cant-get-multiple-images-from-video-using-mpmovieplayercontroller-osstatus-12作为参考)。但是,我意识到该方法与主线程异步工作,因此我的通知处理程序方法似乎从未被调用。
这就是我现在所拥有的。
self.videoURL = info[UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThumbnailImageRequestFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:player];
[player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame];
然后是我的处理程序方法:
-(void)handleThumbnailImageRequestFinishNotification:(NSNotification*)notification
{
NSDictionary *userinfo = [notification userInfo];
NSError* value = [userinfo objectForKey:MPMoviePlayerThumbnailErrorKey];
if (value != nil)
{
NSLog(@"Error creating video thumbnail image. Details: %@", [value debugDescription]);
}
else
{
UIImage *thumbnail = [userinfo valueForKey:MPMoviePlayerThumbnailImageKey];
}
但处理程序永远不会被调用(或者看起来是这样)。