mikaoj/BSImagePicker

Get thumbnail image for video

huy-lv opened this issue · 3 comments

Hi, How to get thumbnail image after picking video from library

Thank you, short question if someone needs

let manager = PHImageManager.default()
        let option = PHImageRequestOptions()
        var thumbnail = UIImage()
        option.isSynchronous = true
        manager.requestImage(for: asset, targetSize: CGSize(width: 138, height: 138), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
            thumbnail = result!
        })
        cell.imageView.image = thumbnail

let option = PHImageRequestOptions()

I recommend enabling isNetworkAccessAllowed. The asset can be stored in iCloud.

var thumbnail = UIImage()

Why?

option.isSynchronous = true

And as assets may be on iCloud, never do synchronous fetching it can freeze the app.

thumbnail = result!

Don't forcefully unwrap. Result can be nil and will crash.
Also as asset can be on iCloud you can get multiple callbacks, first with a low res image and later with the requested image size.

cell.imageView.image = thumbnail

Do that in the image callback instead, but make sure you are on main.