Unable to generate thumbnail from video in gallery
Use UIImagePickerController.InfoKey.mediaURL
to key into the info dictionary. UIImagePickerController.InfoKey.referenceURL
is deprecated.
func didSelectVideo(at url: URL) {
let asset = AVAsset(url: url)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let cgImage = try! generator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
cell.image.image = thumbnail
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true) {
guard let url = info[.mediaURL] as? URL else { return }
self.didSelectVideo(at: url)
}
}
Thumbnail error occurred in UITableView
If you're using youtube
API that is not giving you video thumbnail url?
I can see the URL you're passing to getting the thumbnail is already a thumbnail url so you don't need to do anything extra. The reason is of your error is same you're passing an image url to AVURLAsset
which expects a video url instead. So it can not be open.
You just need to use SDWebImage or any other third party library to showing the image on imageView
. It handles the cache and asynchronous downloading itself.
Example of SDWebImage:
cell.imageForMain.sd_setImage(with: URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
Cannot generate Video Thumbnails with Cache in Swift
You can use the URLCache
:
func previewImageFromVideo(url: NSURL) -> UIImage? {
let url = url as URL
let request = URLRequest(url: url)
let cache = URLCache.shared
if
let cachedResponse = cache.cachedResponse(for: request),
let image = UIImage(data: cachedResponse.data)
{
return image
}
let asset = AVAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
imageGenerator.maximumSize = CGSize(width: 250, height: 120)
var time = asset.duration
time.value = min(time.value, 2)
var image: UIImage?
do {
let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
image = UIImage(cgImage: cgImage)
} catch { }
if
let image = image,
let data = UIImagePNGRepresentation(image),
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)
{
let cachedResponse = CachedURLResponse(response: response, data: data)
cache.storeCachedResponse(cachedResponse, for: request)
}
return image
}
Related Topics
My Data Changes in Uitableviewcell When I Scroll Down and Get Back
How to Continue Ble Activities Onto Next View Controller
When Should I Use Optionals and When Should I Use Non-Optionals with Default Values
Realm Mobile Platform, How to Connect While Offline
Resize a Sklabelnode Font Size to Fit
Swiftui Onhover Doesn't Register Mouse Leaving the Element If Mouse Moves Too Fast
How to Properly Implement the Equatable Protocol in a Class Hierarchy
Scenekit - Animation with Dae File Format
Using Nstimer in Swift Playground
Uitesting Xcode 7: How to Tell If Xcuielement Is Visible
How to Remove Numbers from a String in Swift
Ycombinator Not Working in Swift
Display All Available Wifi Connections with Swift in Os X
How to Connect Localhost (With Invalid Certificate) Using Alamofire
Hstack with Sf Symbols Image Not Aligned Centered