UIImageView setImageFromURL - Swift
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
// | |
// ImageFromUrl.swift | |
// | |
// Created by Francesco Perrotti-Garcia on 1/19/15. | |
// Copyright (c) 2015 Francesco Perrotti-Garcia. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import ObjectiveC | |
private var activityIndicatorAssociationKey: UInt8 = 0 | |
extension UIImageView { | |
var activityIndicator: UIActivityIndicatorView! { | |
get { | |
return objc_getAssociatedObject(self, &activityIndicatorAssociationKey) as? UIActivityIndicatorView | |
} | |
set(newValue) { | |
objc_setAssociatedObject(self, &activityIndicatorAssociationKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN)) | |
} | |
} | |
private func ensureActivityIndicatorIsAnimating() { | |
if (self.activityIndicator == nil) { | |
self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) | |
self.activityIndicator.hidesWhenStopped = true | |
let size = self.frame.size; | |
self.activityIndicator.center = CGPoint(x: size.width/2, y: size.height/2); | |
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in | |
self.addSubview(self.activityIndicator) | |
self.activityIndicator.startAnimating() | |
}) | |
} | |
} | |
convenience init(URL: NSURL, errorImage: UIImage? = nil) { | |
self.init() | |
self.setImageFromURL(URL) | |
} | |
func setImageFromURL(URL: NSURL, errorImage: UIImage? = nil) { | |
self.ensureActivityIndicatorIsAnimating() | |
let downloadTask = NSURLSession.sharedSession().dataTaskWithURL(URL) {(data, response, error) in | |
if (error == nil) { | |
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in | |
self.activityIndicator.stopAnimating() | |
self.image = UIImage(data: data) | |
}) | |
} | |
else { | |
self.image = errorImage | |
} | |
} | |
downloadTask.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment