我正在使用这个作为参考:从IPhone SDK中的视频url或数据中获取缩略图
方法是使用MPMoviePlayerController类而不是AVFoundation,我想我也想使用它,因为人们说MPMoviePlayer方法比AVFoundation方法更快。
问题是,用于创建缩略图的方法,[player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]在iOS 7.0中被废弃。
通过查看苹果文档,剩下的创建缩略图的支持方法是(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引用?
编辑我想出了如何为缩略图图像请求创建通知(使用这个问题作为参考)。但是,我意识到这个方法是从主线程异步工作的,因此我的通知处理程序方法似乎从未被调用过。
这就是我现在所拥有的。
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];
}但是处理程序从来没有被调用过(或者看起来是这样)。
发布于 2013-11-21 12:32:58
试试这边。
import AVFoundation framework 在*.h
#import <AVFoundation/AVFoundation.h>在*.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];发布于 2014-11-28 08:32:14
这里是一个代码,用来制作视频的缩略图,并将图像保存到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];发布于 2013-11-19 12:18:30
如果您的URL是到HTTP活动流,那么它将不会返回任何东西,每个文档。对于一个文件URL,我发现我必须在播放电影之后启动请求,否则它将永远不会被调用。
https://stackoverflow.com/questions/19368513
复制相似问题