2

在我的应用程序中,我想生成视频的多个缩略图,最好是质量好的缩略图。我的旧方法是执行 15 次循环,并在不同时间复制 CGImage。如下所示

func generateThumbnails(_ fileURL:URL) {
    let asset = AVAsset(url: fileURL)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    imageGenerator.apertureMode = AVAssetImageGeneratorApertureMode.cleanAperture
    imageGenerator.appliesPreferredTrackTransform = true
    let duration = asset.duration
    let seconds = CMTimeGetSeconds(duration)
    let addition = seconds / 15
    var number = 1.0
    do {
        while number < seconds {
            let thumbnailCGImage = try imageGenerator.copyCGImage(at: CMTimeMake(Int64(number),1), actualTime: nil)
            let image = UIImage(cgImage: thumbnailCGImage)
            thumbnails.append(image)
            number += addition
        }
    } catch let err {
        return
    }
}

但是,在做了更多研究之后,我发现使用异步生成缩略图更合乎逻辑

let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.generateCGImagesAsynchronously(forTimes: [NSValue], completionHandler: AVAssetImageGeneratorCompletionHandler)

但是,我真的不知道我应该在 [NSValue] 和完成处理程序中输入什么。

我只需要解释如何以这种方式生成缩略图,看看这是否更好。

4

1 回答 1

4

https://developer.apple.com/documentation/avfoundation/avassetimagegenerator/1388100-generatecgimagesasynchronously

requestedTimes一个 NSValue 对象数组,每个对象都包含一个 CMTime,指定请求图像的资产时间。

用法:

let duration = asset.duration
let seconds = CMTimeGetSeconds(duration)
let addition = seconds / 15
var number = 1.0

var times = [NSValue]()
times.append(NSValue(time: CMTimeMake(Int64(number), 1)))
while number < seconds {
    number += addition
    times.append(NSValue(time: CMTimeMake(Int64(number), 1)))
}

struct Formatter {
    static let formatter: DateFormatter = {
        let result = DateFormatter()
        result.dateStyle = .short
        return result
    }()
}

imageGenerator.generateCGImagesAsynchronously(forTimes: times) { (requestedTime, cgImage, actualImageTime, status, error) in

    let seconds = CMTimeGetSeconds(requestedTime)
    let date = Date(timeIntervalSinceNow: seconds)
    let time = Formatter.formatter.string(from: date)

    switch status {
    case .succeeded: do {
            if let image = cgImage {
                print("Generated image for approximate time: \(time)")

                let img = UIImage(cgImage: image)
                //do something with `img`
            }
            else {
                print("Failed to generate a valid image for time: \(time)")
            }
        }

    case .failed: do {
            if let error = error {
                print("Failed to generate image with Error: \(error) for time: \(time)")
            }
            else {
                print("Failed to generate image for time: \(time)")
            }
        }

    case .cancelled: do {
        print("Image generation cancelled for time: \(time)")
        }
    }
}
于 2018-01-01T03:46:39.100 回答