I have made a library collectionview filled with videos i fetch from the Photos framework, i use thumbnails to display all the different videos using the following method
func requestImageForAsset(_ asset: PHAsset!, targetSize targetSize: CGSize, contentMode contentMode: PHImageContentMode, options options: PHImageRequestOptions!, resultHandler resultHandler: ((UIImage!, [NSObject : AnyObject]!) -> Void)!) -> PHImageRequestID
which works fine. But my videos always start with a black frame, so all the thumbnails are black. can I offset the frame from which it takes a snapshot image?
This is code i have used in my project which works like a charm for me
func generateThumnail(url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.maximumSize = CGSize(width: 100, height: 100)
let time = CMTimeMake(1, 30)
if let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil) {
return UIImage(cgImage: img)
}
return nil
}
The above function will return the image at 1 sec of the video
This is the way of passing the assetUrl of your video to get the thumbnail
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! String
if (mediaType == kUTTypeMovie as! String){
let tempVideo = info[UIImagePickerControllerMediaURL] as! URL
if let thumbnail = self.generateThumbnail(url: tempVideo) {
// Use your thumbnail
}