Search code examples
iosswiftvideosdwebimage

Fetch Thumbnail from Video URL using SDWebImage


I need to fetch thumbnail from a video URL to display in ImageView that is in a UITableViewCell.

I can get thumbnail using this method but it takes lots of resource and hence the tableview scrolling lags and stops for the time cellForRowAtIndexPath is called.

This is the method.

func createThumbnailOfVideoFromFileURL(videoURL: String) -> UIImage? {
    let asset = AVAsset(url: URL(string: videoURL)!)
    let assetImgGenerate = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    let time = CMTimeMakeWithSeconds(Float64(1), 100)
    do {
        let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
        let thumbnail = UIImage(cgImage: img)
        return thumbnail
    } catch {
        return UIImage(named: "ico_placeholder")
    }
}

Can anyone please suggest me an alternative to use SDWebImage so that the images go to the cache and are not fetched everytime UITableView delegates are called. Kindly suggest any other approach if i m wrong.


Solution

  • I used the above method as it is and cached the images in an array of UIImage and made a check in cellForRowAtIndexPath that if image exists at that particular index then use that else fetch the image in background.