Last active
September 10, 2016 14:58
-
-
Save xmkevinchen/8f8d12e1a58d6f4bbe1bf4743841e196 to your computer and use it in GitHub Desktop.
iOS: Getting image size without downloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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