Skip to content

Instantly share code, notes, and snippets.

@filsv
Last active September 20, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filsv/5393f9b5719cd0ff7fe0834a6602dd3b to your computer and use it in GitHub Desktop.
Save filsv/5393f9b5719cd0ff7fe0834a6602dd3b to your computer and use it in GitHub Desktop.
Image thumbnail from video
import AVFoundation
func getThumbnail(url: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: url , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch (let error) {
debugPrint("Error with thumbnail generation: \(error.localizedDescription)")
return nil
}
}
@filsv
Copy link
Author

filsv commented Sep 20, 2019

Not supported on WatchOS (currently 6.0)
Supported on: iOS 4.0+, macOS 10.7+, tvOS 9.0+;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment