Last active
May 17, 2018 07:30
-
-
Save iAmrSalman/bc9df34c533d1bef75e8c1fc6a3a351b to your computer and use it in GitHub Desktop.
[Generate Video Thumbnail] function to generate video thumbnail using AVFoundation #avfoundation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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