Get thumbnail from video URL in swift
Generating thumbnail or preview image from video URL in your iOS app seems to be a tough task, but its not. In this tutorial we will look at the code snippet to create or generate thumbnail from video url..
If you are looking for objective C code then please go to this link How to get thumbnail from video url in Objective C – Tutorial
Swift code to generate thumbnail from video URL:
Below is the code that will generate thumbnail from a video URL.
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
DispatchQueue.global().async { | |
let asset = AVAsset(url: url) | |
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) | |
assetImgGenerate.appliesPreferredTrackTransform = true | |
let time = CMTimeMake(1, 2) | |
let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil) | |
if img != nil { | |
let frameImg = UIImage(cgImage: img!) | |
DispatchQueue.main.async(execute: { | |
// assign your image to UIImageView | |
}) | |
} | |
} |
Where to go from here:
In this tutorial you learned how to create or generate thumbnail from video url. The code is fully testified at the time of tutorial being written. If any one found issue is code then please post it as comment. We will try to fix it as soon as we can. Thanks for reading. Happy coding
Pingback: Getting thumbnail from video url or data - iOSTutorialJunction