iOS – 如何在没有游戏的情况下从视频获取缩略图?

iOS – 如何在没有游戏的情况下从视频获取缩略图?,第1张

概述我正在尝试从视频中获取缩略图并在我的tableview中显示它.这是我的代码: - (UIImage *)imageFromVideoURL:(NSURL *)contentURL { AVAsset *asset = [AVAsset assetWithURL:contentURL]; // Get thumbnail at the very start of the vid 我正在尝试从视频中获取缩略图并在我的tablevIEw中显示它.这是我的代码:
- (UIImage *)imageFromVIDeoURL:(NSURL *)contentURL {    AVAsset *asset = [AVAsset assetWithURL:contentURL];    //  Get thumbnail at the very start of the vIDeo    CMTime thumbnailTime = [asset duration];    thumbnailTime.value = 25;    //  Get image from the vIDeo at the given time    AVAssetimageGenerator *imageGenerator = [[AVAssetimageGenerator alloc] initWithAsset:asset];    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NulL error:NulL];    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];    CGImageRelease(imageRef);    return thumbnail;}

但是图像总是回归黑色.怎么了?

解决方法 用这个 :
斯威夫特3 –
func createthumbnailOfVIDeoFromfileURL(vIDeoURL: String) -> UIImage? {    let asset = AVAsset(url: URL(string: vIDeoURL)!)    let assetimgGenerate = AVAssetimageGenerator(asset: asset)    assetimgGenerate.applIEsPreferredTracktransform = true    let time = CMTimeMakeWithSeconds(float64(1),100)    do {        let img = try assetimgGenerate.copyCGImage(at: time,actualTime: nil)        let thumbnail = UIImage(cgImage: img)        return thumbnail    } catch {        return UIImage(named: "ico_placeholder")    }}

重要的提示 :

你需要在if else中使用它,因为它是资源广泛的.您必须将图像存储在数组或模型中,并检查是否一旦创建了缩略图,它就会引用缓存/数组,以便cellForRowAtIndexPath不会导致滚动UItableVIEw的延迟

总结

以上是内存溢出为你收集整理的iOS – 如何在没有游戏的情况下从视频获取缩略图?全部内容,希望文章能够帮你解决iOS – 如何在没有游戏的情况下从视频获取缩略图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1107520.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存