title: What to input to Generate CGImages Asynchronously
body: <p>In my app, I want to generate multiple thumbnails of a video, preferably good quality thumbnails. My old approach was to perform a loop 15 times, and copy a CGImage at different time. As shown below</p>
  
  <pre><code>func generateThumbnails(_ fileURL:URL) {
      let asset = AVAsset(url: fileURL)
      let imageGenerator = AVAssetImageGenerator(asset: asset)
      imageGenerator.apertureMode = AVAssetImageGeneratorApertureMode.cleanAperture
      imageGenerator.appliesPreferredTrackTransform = true
      let duration = asset.duration
      let seconds = CMTimeGetSeconds(duration)
      let addition = seconds / 15
      var number = 1.0
      do {
          while number &lt; seconds {
              let thumbnailCGImage = try imageGenerator.copyCGImage(at: CMTimeMake(Int64(number),1), actualTime: nil)
              let image = UIImage(cgImage: thumbnailCGImage)
              thumbnails.append(image)
              number += addition
          }
      } catch let err {
          return
      }
  }
  </code></pre>
  
  <p>However, after doing some more research, I found it was more logical to just generate thumbnails asynchronously using</p>
  
  <pre><code>let imageGenerator = AVAssetImageGenerator(asset: asset)
  imageGenerator.generateCGImagesAsynchronously(forTimes: [NSValue], completionHandler: AVAssetImageGeneratorCompletionHandler)
  </code></pre>
  
  <p>However, I don't really know what the I am supposed to input into the [NSValue] and the completion handler.</p>
  
  <p>I just need an explanation on how to generate thumbnails this way, and see if it's the better thing to do.</p>