我使用此作为参考:Getting thumbnail from a video url or data in 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 = ???
如何获得缩略图的UIImage引用?
修改 我想出了如何为缩略图图像请求创建通知(使用this question作为参考)。但是,我意识到这个方法与主线程异步工作,因此似乎没有调用我的通知处理程序方法。
这就是我现在所拥有的。
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];
}
但处理程序永远不会被调用(或者它出现)。
答案 0 :(得分:15)
试试这种方式。
import AVFoundation framework
in * .h
#import <AVFoundation/AVFoundation.h>
in * .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];
答案 1 :(得分:3)
以下是制作视频缩略图并将图像保存到DocumentDirectory的代码。
//pass the video_path to NSURL
NSURL *videoURL = [NSURL fileURLWithPath:strVideoPath];
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generator.appliesPreferredTrackTransform = YES;
//Set the time and size of thumbnail for image
NSError *err = NULL;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
CGSize maxSize = CGSizeMake(425,355);
generator.maximumSize = maxSize;
CGImageRef imgRef = [generator copyCGImageAtTime:thumbTime actualTime:NULL error:&err];
UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
//And you can save the image to the DocumentDirectory
NSData *data = UIImagePNGRepresentation(thumbnail);
//Path for the documentDirectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[data writeToFile:[documentsDirectory stringByAppendingPathComponent:currentFileName] atomically:YES];
答案 2 :(得分:1)
如果您的网址是HTTP直播流,那么根据文档,它不会返回任何内容。对于文件URL,我发现我必须在播放电影后启动请求,否则它将永远不会被调用。