gpt4 book ai didi

objective-c - 从视频生成缩略图 - ios7

转载 作者:太空狗 更新时间:2023-10-30 03:46:51 25 4
gpt4 key购买 nike

我正在使用它作为引用:Getting thumbnail from a video url or data in IPhone SDK

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

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

通过查看苹果文档,其余支持的创建缩略图的方法是方法 (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option(void )取消AllThumbnailImageRequests。但是,正如方法签名所指示的那样,这些方法不返回任何内容。那么如何访问通过这些方法创建的 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];
}

但是处理程序永远不会被调用(或者看起来如此)。

最佳答案

试试这个方法。

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

关于objective-c - 从视频生成缩略图 - ios7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com