gpt4 book ai didi

ios - AVFoundation:copyCGImageAtTime 总是返回 nil

转载 作者:行者123 更新时间:2023-11-28 08:01:10 33 4
gpt4 key购买 nike

我正在尝试从视频中获取所有帧,但它总是在

cgImage = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)

我当前的代码是这样的

if let path = NSBundle.mainBundle().resourcePath {
let fullPath = path + "/mov/watch_phone.mov"
let url = NSURL(fileURLWithPath: fullPath)
mutableVideoURL = url
getFrame()
print(videoFrames.count)
}

func getFrame() {
let asset:AVAsset = AVAsset(URL:self.mutableVideoURL as NSURL)
let assetImgGenerate:AVAssetImageGenerator = AVAssetImageGenerator(asset:asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let duration:Float64 = CMTimeGetSeconds(asset.duration)
let durationInt:Int = Int(duration)

for index:Int in 0 ..< durationInt
{
generateFrames(assetImgGenerate, fromTime: Float64(index))
}
}

func generateFrames(assetImgGenerate:AVAssetImageGenerator, fromTime:Float64)
{
let time:CMTime = CMTimeMakeWithSeconds(fromTime, 600)
let cgImage:CGImage?

do
{
cgImage = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
let frameImg:UIImage = UIImage(CGImage:cgImage!)
videoFrames.append(frameImg)
}
catch let error as NSError {

print("Found error in saving entity changes \(error)")
}

copyCGImageAtTime 总是返回 nil。该视频已添加到我的 bundle 中,并且还出现在构建阶段的捆绑资源中。

这是在 catch block 中收到的错误

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x1700563e0 {Error Domain=NSOSStatusErrorDomain Code=-12430 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12430), NSLocalizedDescription=The operation could not be completed}

请帮助我理解我在这里做错了什么

最佳答案

这是一个工作代码。试试这个,确保你有非零 Assets ,正确的时间。

- (UIImage *)getUIImageOfVideoAsset:(AVAsset *)asset atTime:(CMTime)time withSize:(CGSize)size {

NSError *error;
AVAssetImageGenerator *baseImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
baseImageGenerator.appliesPreferredTrackTransform = YES;
baseImageGenerator.maximumSize = size;

CGImageRef cgImage = [baseImageGenerator copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);

return image;
}

关于ios - AVFoundation:copyCGImageAtTime 总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645675/

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