我正在开发一个正在使用的 OS X 应用程序,AVAssetImageGenerator.generateCGImagesAsynchronouslyForTimes
它通常可以正常工作。但是,有时我得到的缩略图只包含前几行像素,其余的都是绿色的,有时图像会是不同深浅的绿色。很难追踪,因为它不会一直发生,但当它发生时,大约一半的缩略图会受到影响。这是我期望看到的图像:
但经常会发生这种情况:
这是我用来生成缩略图的代码:
let assetGenerator = AVAssetImageGenerator(asset: AVURLAsset(URL: url))
assetGenerator.appliesPreferredTrackTransform = true
let time = CMTime(seconds: 0, preferredTimescale: 30)
let handler: AVAssetImageGeneratorCompletionHandler = { _, image, _, res, error in
defer { dispatch_group_leave(self.waitForThumbnail!) }
guard let image = image where res == .Succeeded else {
if let error = error { print(error) }
return
}
let s = CGSize(width: CGImageGetWidth(image), height: CGImageGetHeight(image))
self.thumbnail = NSImage(CGImage: image, size: s)
}
waitForThumbnail = dispatch_group_create()
dispatch_group_enter(waitForThumbnail!)
assetGenerator.maximumSize = maxThumbnailSize
assetGenerator.generateCGImagesAsynchronouslyForTimes([NSValue(CMTime: time)], completionHandler: handler)
这就是我检索缩略图的方式:
dispatch_group_wait(file.waitForThumbnail!, DISPATCH_TIME_FOREVER)
dispatch_async(dispatch_get_main_queue()) {
self.imageView.image = file.thumbnail
}
非常感谢任何帮助,谢谢!