Thumbnail generating is slow in ios

802 views Asked by At

I'm working on showing the thumbnails of the videos. Here is my code.

override func viewDidLoad() {
    super.viewDidLoad()
 for str in self.imgArray
        {
            let url = NSURL(string: str)
            let movieAsset = AVURLAsset(URL: url!, options: nil)
            let assetImageGemerator = AVAssetImageGenerator(asset: movieAsset)
            assetImageGemerator.appliesPreferredTrackTransform = true
           let frameRef = try! assetImageGemerator.copyCGImageAtTime(CMTimeMake(1, 2), actualTime: nil)
            let image = UIImage(CGImage: frameRef)
            self.imagesArray.append(image)
        }
}

By using this I'm getting thumbnails correctly. The issue is that there is a delay of about 5-10 seconds in generating the thumbnail image. Is there anyway that I could improve the speed of this code and generate the thumbnail fastly?

1

There are 1 answers

0
christian On

I don't think there will be a way to actually speed up the code - try with CMTimeMake(0, 10). Maybe it will faster the code since some video files takes some time to seek.

I think you need to cache images you got from the code and consult the cached images next time so that it runs faster overall. There are a lot of ways to cache images - using NSCache is an option.

As a side note, it won't take 5-10 seconds to get thumbnail images. It took less than one second usually.