Thumbnail from UiImagepicker video

时间:2017-10-12 10:09:11

标签: ios swift ios11

I picked a video from album using UIImagePickerController and trying to generate thumbnail from it using below function:

func getThumbnailFrom(path: URL) -> UIImage? {

        do {

            let asset = AVURLAsset.init(url: path)
            print(asset.url)
            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 {

            print("*** Error generating thumbnail: \(error.localizedDescription)")
            return nil

        }

    }

But on logs I van see *** Error generating thumbnail: Cannot Open

[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

Any idea why ? Its the bug in iOS 11 devices, any idea how to fix this issue and generate thumbnail ?

Thanks!

1 个答案:

答案 0 :(得分:0)

Okay, this is about asking permissions which I already asked at the beginning of my app. Still I need to ask again, I dont know why, but it worked.

PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
            ()

            if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
                print("creating 2")
                if let thumbnailImage = self.getThumbnailFrom(path: self.facebookVideoURL){
                    self.thumbnailImageForVideo.image = thumbnailImage
                }
            }

        })