Created
January 3, 2016 16:59
-
-
Save Athosone/1b68a1a009908ed0dd53 to your computer and use it in GitHub Desktop.
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
func associatedObject<T: AnyObject>( | |
base: AnyObject, | |
key: UnsafePointer<UInt8>, | |
initialiser: () -> T) | |
-> T { | |
if let associated = objc_getAssociatedObject(base, key) | |
as? T { return associated } | |
let associated = initialiser() | |
objc_setAssociatedObject(base, key, associated, | |
.OBJC_ASSOCIATION_RETAIN) | |
return associated | |
} | |
func associateObject<T: AnyObject>( | |
base: AnyObject, | |
key: UnsafePointer<UInt8>, | |
value: T) { | |
objc_setAssociatedObject(base, key, value, | |
.OBJC_ASSOCIATION_RETAIN) | |
} | |
private var fileKey: UInt8 = 0 | |
extension UIImageView { | |
var file:PFFile { | |
get { | |
return associatedObject(self, key: &fileKey) { self.file } | |
} | |
set { | |
associateObject(self, key: &fileKey, value: newValue) | |
self.loadImage() | |
} | |
} | |
private func loadImage() | |
{ | |
var aiView:UIActivityIndicatorView? | |
for view in self.subviews | |
{ | |
if let aiv = view as? UIActivityIndicatorView | |
{ | |
aiView = aiv | |
} | |
} | |
aiView?.startAnimating() | |
file.getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in | |
if let imageDatas = data | |
{ | |
let imageFromDatas = UIImage(data: imageDatas) | |
dispatch_async(dispatch_get_main_queue()) { () -> Void in | |
aiView?.stopAnimating() | |
self.image = imageFromDatas | |
} | |
} | |
else if let err = error | |
{ | |
print("[ERROR] loading image in UIImageView with error: " + err.localizedDescription) | |
} | |
} | |
} | |
} | |
extension UIImage { | |
var highestQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 1.0)! } | |
var highQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 0.75)!} | |
var mediumQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 0.5)! } | |
var lowQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 0.25)!} | |
var lowestQualityJPEGNSData:NSData { return UIImageJPEGRepresentation(self, 0.0)! } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also add stored property to swift extension