Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension AVAsset {
private func _generator(size: CGSize) -> AVAssetImageGenerator {
let generator = AVAssetImageGenerator(asset: self)
generator.maximumSize = size
generator.appliesPreferredTrackTransform = true
return generator
}
func extractUIImageAsync(size: CGSize) -> Single<UIImage?> {
let generator = _generator(size: size)
return .create { observer in
generator.generateCGImagesAsynchronously(forTimes: [NSValue(time: .zero)]) { (_, image, _, _, error) in
if let error = error {
observer(.error(error))
return
}
observer(.success(image.map(UIImage.init(cgImage:))))
}
return Disposables.create {
generator.cancelAllCGImageGeneration()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment