Skip to content

Instantly share code, notes, and snippets.

@dmytro-anokhin
Created November 17, 2019 21:36
Show Gist options
  • Select an option

  • Save dmytro-anokhin/9711f9886b73fa35b094652a17ac9d57 to your computer and use it in GitHub Desktop.

Select an option

Save dmytro-anokhin/9711f9886b73fa35b094652a17ac9d57 to your computer and use it in GitHub Desktop.
func createFrameImage(at index: Int, subsamplingLevel: SubsamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage? {
guard index < frameCount else {
return nil
}
let image: CGImage?
let options: CFDictionary
switch decodingOptions.mode {
case .asynchronous:
// Don't consider the subsamplingLevel when comparing the image native size with sizeForDrawing.
guard var size = frameSize(at: index) else {
return nil
}
if let sizeForDrawing = decodingOptions.sizeForDrawing {
// See which size is smaller: the image native size or the sizeForDrawing.
if sizeForDrawing.width * sizeForDrawing.height < size.width * size.height {
size = sizeForDrawing
}
}
options = imageSourceAsyncOptions(sizeForDrawing: size, subsamplingLevel: subsamplingLevel)
image = CGImageSourceCreateThumbnailAtIndex(imageSource, index, options)
case .synchronous:
options = imageSourceOptions(with: subsamplingLevel)
image = CGImageSourceCreateImageAtIndex(imageSource, index, options)
}
// WebKit has support for xbm images but we don't
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment