Skip to content

Instantly share code, notes, and snippets.

@xmkevinchen
Last active September 10, 2016 14:58
Show Gist options
  • Save xmkevinchen/8f8d12e1a58d6f4bbe1bf4743841e196 to your computer and use it in GitHub Desktop.
Save xmkevinchen/8f8d12e1a58d6f4bbe1bf4743841e196 to your computer and use it in GitHub Desktop.
iOS: Getting image size without downloading
import ImageIO
func imageSize(with url: NSURL) -> CGSize {
var size: CGSize = .zero
let source = CGImageSourceCreateWithURL(url, nil)!
let options = [kCGImageSourceShouldCache as String: false]
if let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, options as CFDictionary?) {
if let properties = properties as? [String: Any],
let width = properties[kCGImagePropertyPixelWidth as String] as? Int,
let height = properties[kCGImagePropertyPixelHeight as String] as? Int {
size = CGSize(width: width, height: height)
}
}
return size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment