Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Last active May 17, 2018 07:30
Show Gist options
  • Save iAmrSalman/bc9df34c533d1bef75e8c1fc6a3a351b to your computer and use it in GitHub Desktop.
Save iAmrSalman/bc9df34c533d1bef75e8c1fc6a3a351b to your computer and use it in GitHub Desktop.
[Generate Video Thumbnail] function to generate video thumbnail using AVFoundation #avfoundation
//Don't forget to import AVFoundation
func getVideoThumbnail(from path: URL?) -> UIImage? {
guard let path = path else { return nil }
do {
let asset = AVURLAsset(url: path , 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 {
print("*** Error generating thumbnail: \(error.localizedDescription)")
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment