缩略图图像时间:现在已弃用 - 有什么替代方法? [英] thumbnailImageAtTime: now deprecated - What's the alternative?
问题描述
直到 iOS7 更新我使用...
Until iOS7 update I was using...
UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
...取得了巨大的成功,因此我的应用可以显示用户刚刚拍摄的视频的静止图像.
...with great success, so that my app could show a still of the video that the user had just taken.
我理解这种方法,因为 iOS7 现在已弃用,我需要一个替代方法.我看到有一种方法
I understand this method, as of iOS7 has now deprecated and I need an alternative. I see there's a method of
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option
虽然我如何从中返回图像以便将其放置在 videoReview 按钮图像中?
though how do I return the image from it so I can place it within the videoReview button image?
提前致谢,吉姆.
****编辑问题,尝试通知中心方法后***
****Edited question, after trying notification centre method***
我使用了以下代码 -
[moviePlayer requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionNearestKeyFrame];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerThumbnailImageRequestDidFinishNotification::) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:moviePlayer];
我制作了两个 NSNumber 对象的 NSArray 时间 1 &2.
I made the NSArray times of two NSNumber objects 1 & 2.
然后我尝试使用以下方法捕获通知
I then tried to capture the notification in the following method
-(void)MPMoviePlayerThumbnailImageRequestDidFinishNotification: (NSDictionary*)info{
UIImage *image = [info objectForKey:MPMoviePlayerThumbnailImageKey];
然后继续使用此缩略图作为按钮图像作为预览....但它不起作用.
Then proceeded to use this thumbnail image as the button image as a preview.... but it didn't work.
如果您能从我的编码中看到我出错的地方,我们将再次感谢您的帮助.干杯
If you can see from my coding where I've went wrong your help would be appreciated again. Cheers
推荐答案
设法找到了使用 AVAssetImageGenerator 的好方法,请参阅下面的代码...
Managed to find a great way using AVAssetImageGenerator, please see code below...
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:partOneUrl options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *one = [[UIImage alloc] initWithCGImage:oneRef];
[_firstImage setImage:one];
_firstImage.contentMode = UIViewContentModeScaleAspectFit;
在头文件中,请导入
#import <AVFoundation/AVFoundation.h>
它运行良好,我已经能够从 viewDidLoad 调用它,这比从 viewDidAppear 调用已弃用的 thumbNailImageAtTime: 更快.
It works perfect and I've been able to call it from viewDidLoad, which was quicker than calling the deprecated thumbNailImageAtTime: from the viewDidAppear.
希望这能帮助遇到同样问题的其他人.
Hope this helps anyone else who had the same problem.
* ** Swift 5.1 更新 ****
有用的功能...
func createThumbnailOfVideoUrl(url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(1.0, preferredTimescale: 600)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error.localizedDescription)
return nil
}
}
这篇关于缩略图图像时间:现在已弃用 - 有什么替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!