从视频生成缩略图 - ios7 [英] Generating thumbnail from video - ios7

查看:129
本文介绍了从视频生成缩略图 - ios7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此作为参考:从视频中获取缩略图IPhone SDK中的网址或数据

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

The method is using the MPMoviePlayerController class instead of the AVFoundation, and I think I want to use that as well because the people said that MPMoviePlayer way is faster than the AVFoundation way.

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

The problem is, the method used to create the thumbnails, [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame] is deprecated in iOS 7.0.

通过查看apple文档,其余支持的创建缩略图的方法是通过方法(void)requestThumbnailImagesAtTimes :( NSArray *)playbackTimes timeOption:(MPMovieTimeOption)选项(void)cancelAllThumbnailImageRequests 。但是,正如方法签名所规定的那样,这些方法不会返回任何内容。那么如何访问由这些方法创建的 UIImage 缩略图?

By looking at the apple docs, the remaining supported ways to create thumbnails are by the methods (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option and (void)cancelAllThumbnailImageRequests. But, as the method signatures dictate, these methods return nothing. So how do I access the UIImage thumbnail created by these methods?

如果有帮助,这就是我所拥有的到目前为止在代码方面:

If it helps, this is what I have so far in terms of code:

    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参考?

How do I get a UIImage reference to the thumbnail?

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

EDIT I figured out how to create a notification for the thumbnail image request (using this question as reference). However, I realise that this method works asynchronously from the main thread, and so my notification handler method doesn't seem to ever be called.

这就是我现在拥有的。

    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];

然后我的处理程序方法:

And then my handler method:

-(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];
}

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

But the handler never gets called (or so it appears).

推荐答案

试试这种方式。

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];

这篇关于从视频生成缩略图 - ios7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆