Created
November 5, 2015 11:33
-
-
Save ilyapuchka/90bd3954c673cc55ad4e to your computer and use it in GitHub Desktop.
CFDictionary and Swift 2.1
This file contains hidden or 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
//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