Skip to content

Instantly share code, notes, and snippets.

@varun04
Last active August 4, 2021 11:56
Show Gist options
  • Select an option

  • Save varun04/11795320a0c40e7ee7709b73134b2848 to your computer and use it in GitHub Desktop.

Select an option

Save varun04/11795320a0c40e7ee7709b73134b2848 to your computer and use it in GitHub Desktop.
func createFrameImage(at index: Int, downsamplingLevel: DownSamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage? {
guard index < frameCount else {
return nil
}
let image: CGImage?
let options: CFDictionary
switch decodingOptions.mode {
case .asynchronous:
// No need to consider the down sampling when comparing the image native size with sizeForDrawing.
guard var size = frameSize(at: index) else {
return nil
}
if let sizeForDrawing = decodingOptions.sizeForDrawing {
// Choose the smaller one.
if sizeForDrawing.width * sizeForDrawing.height < size.width * size.height {
size = sizeForDrawing
}
}
options = imageSourceAsyncOptions(sizeForDrawing: size, donwsamplingLevel: downsamplingLevel)
image = CGImageSourceCreateThumbnailAtIndex(imageSource, index, options)
case .synchronous:
options = imageSourceOptions(with: downsamplingLevel)
image = CGImageSourceCreateImageAtIndex(imageSource, index, options)
}
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment