Skip to content

Instantly share code, notes, and snippets.

@ilyapuchka
Created November 5, 2015 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyapuchka/90bd3954c673cc55ad4e to your computer and use it in GitHub Desktop.
Save ilyapuchka/90bd3954c673cc55ad4e to your computer and use it in GitHub Desktop.
CFDictionary and Swift 2.1
//In Swift 1.x this works and `properties` are casted to `[NSObject: AnyObject]`:
func imageOwnerWithURL(url: NSURL) -> ZMImageOwner! {
if let source = CGImageSourceCreateWithURL(url, nil),
properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [NSObject: AnyObject],
imageWidth = (properties[kCGImagePropertyPixelWidth] as? NSNumber)?.floatValue,
imageHeight = (properties[kCGImagePropertyPixelHeight] as? NSNumber)?.floatValue,
data = NSData(contentsOfURL: url) where acceptableSourceType(source) {
return ImageOwner(data: data, size: CGSizeMake(CGFloat(imageWidth), CGFloat(imageHeight)), nonce: self.nonce)
}
return nil
}
//In Swift 2 as? [NSObject: AnyObject], but this works:
func imageOwnerWithURL(url: NSURL) -> ZMImageOwner! {
if let source = CGImageSourceCreateWithURL(url, nil),
properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as Dictionary?,
imageWidth = (properties[kCGImagePropertyPixelWidth] as? NSNumber)?.floatValue,
imageHeight = (properties[kCGImagePropertyPixelHeight] as? NSNumber)?.floatValue,
data = NSData(contentsOfURL: url) where acceptableSourceType(source) {
return ImageOwner(data: data, size: CGSizeMake(CGFloat(imageWidth), CGFloat(imageHeight)), nonce: self.nonce)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment