Created
December 4, 2016 08:32
-
-
Save nhathm/4a75bb4b754a636436f318d050221ab5 to your computer and use it in GitHub Desktop.
Swift note about AVFoundation
This file contains 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
// Get priview image for video | |
func previewImageFromVideo(_ url: URL) -> UIImage? { | |
let asset = AVAsset(url: url) | |
let imageGenerator = AVAssetImageGenerator(asset: asset) | |
imageGenerator.appliesPreferredTrackTransform = true | |
var time = asset.duration | |
time.value = min(time.value, 2) | |
do { | |
let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil) | |
return UIImage(cgImage: imageRef) | |
} | |
catch let error as NSError | |
{ | |
print("Error: previewImageFromVideo: \(error)") | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment