从视频生成缩略图 - ios7

pla*_*nic 5 video objective-c thumbnails mpmovieplayercontroller ios7

我使用它作为参考:从视频网址获取缩略图或在iPhone SDK中获取数据

该方法使用MPMoviePlayerController类而不是AVFoundation,我想我也想使用它,因为人们说MPMoviePlayer方式比AVFoundation方式更快.

问题是,用于创建缩略图的方法[player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]在iOS 7.0中已弃用.

通过查看apple文档,其余支持的创建缩略图的方法是通过方法(void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option(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 = ???
Run Code Online (Sandbox Code Playgroud)

如何获得缩略图的UIImage引用?

编辑 我想出了如何为缩略图图像请求创建通知(使用此问题作为参考).但是,我意识到这个方法从主线程异步工作,所以我的通知处理程序方法似乎从未被调用过.

这就是我现在拥有的.

    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];
Run Code Online (Sandbox Code Playgroud)

然后是我的处理程序方法:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

但处理程序永远不会被调用(或者它出现).

san*_*alp 15

试试这种方式.

import AVFoundation framework 
Run Code Online (Sandbox Code Playgroud)

在*.h

#import <AVFoundation/AVFoundation.h>
Run Code Online (Sandbox Code Playgroud)

在*.m

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.urlForConevW options:nil];
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
NSLog(@"error==%@, Refimage==%@", error, refImg);

UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg];
Run Code Online (Sandbox Code Playgroud)


Chr*_*ett 1

如果您的 URL 是 HTTP 直播流,那么根据文档,它不会返回任何内容。对于文件 URL,我发现必须在播放电影后启动请求,否则永远不会被调用。