Last active
September 20, 2019 16:35
-
-
Save filsv/5393f9b5719cd0ff7fe0834a6602dd3b to your computer and use it in GitHub Desktop.
Image thumbnail from video
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not supported on WatchOS (currently 6.0)
Supported on: iOS 4.0+, macOS 10.7+, tvOS 9.0+;