iOS: How to retrieve image dimensions without loading CGImage into memory

Updated
Mar 10, 2021 1:59 AM
Created
Mar 9, 2021 6:39 PM
Tags
Objective-CSwiftCoreImageBookmark
Keywords

My version

func size(of imageSource: CGImageSource) -> CGSize? {
  let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
  guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, propertiesOptions) as? [CFString: Any] else {
    return nil
  }
  
  guard
    let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
    let height = properties[kCGImagePropertyPixelHeight] as? CGFloat
  else {
    return nil
  }
  return CGSize(width: width, height: height)
}