Repository: onevcat/Kingfisher
File: Kingfisher/UIImageView+Kingfisher.swift (Swift)

Code before:
    Set an image with a URL, a placeholder image, options, progress handler and completion handler.
    
    :param: URL               The URL of image.
    :param: placeholderImage  A placeholder image when retrieving the image at URL.
    :param: optionsInfo       A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
    :param: progressBlock     Called when the image downloading progress gets updated.
    :param: completionHandler Called when the image retrieved and set.
    
    :returns: A task represents the retriving process.
    */
    public func kf_setImageWithURL(URL: NSURL,
                      placeholderImage: UIImage?,
                           optionsInfo: KingfisherOptionsInfo?,
                         progressBlock: DownloadProgressBlock?,
                     completionHandler: CompletionHandler?) -> RetrieveImageTask
    {
        image = placeholderImage
        
        kf_setWebURL(URL)
        let task = KingfisherManager.sharedManager.retrieveImageWithURL(URL, optionsInfo: optionsInfo, progressBlock: { (receivedSize, totalSize) -> () in
            if let progressBlock = progressBlock {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    progressBlock(receivedSize: receivedSize, totalSize: totalSize)
                })
            }
            }, completionHandler: {[weak self] (image, error, cacheType, imageURL) -> () in
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    if let sSelf = self where imageURL == sSelf.kf_webURL && image != nil {
                        sSelf.image = image;
                    }
                    completionHandler?(image: image, error: error, cacheType:cacheType, imageURL: imageURL)
                })
            })
        
        return task
    }
}

// MARK: - Associated Object
private var lastURLkey: Void?
public extension UIImageView {
    /// Get the image URL binded to this image view.
    public var kf_webURL: NSURL? {
        get {
            return objc_getAssociatedObject(self, &lastURLkey) as? NSURL
        }
    }
    
    private func kf_setWebURL(URL: NSURL) {
        objc_setAssociatedObject(self, &lastURLkey, URL, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
    }

Review comment (suggestion):
``` swift
indicator.autoresizingMask = .FlexibleLeftMargin | .FlexibleRightMargin | .FlexibleBottomMargin | .FlexibleTopMargin
```


Code after:
    
    /**
    Set an image with a URL, a placeholder image, options, progress handler and completion handler.
    
    :param: URL               The URL of image.
    :param: placeholderImage  A placeholder image when retrieving the image at URL.
    :param: optionsInfo       A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
    :param: progressBlock     Called when the image downloading progress gets updated.
    :param: completionHandler Called when the image retrieved and set.
    
    :returns: A task represents the retriving process.
    */
    
    public func kf_setImageWithURL(URL: NSURL,
                      placeholderImage: UIImage?,
                           optionsInfo: KingfisherOptionsInfo?,
                         progressBlock: DownloadProgressBlock?,
                     completionHandler: CompletionHandler?) -> RetrieveImageTask
    {
        
        var indicator = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
        if ( showIndicatorWhenLoading == true)
        {
            indicator = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
            indicator.center = self.center
            indicator.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin|UIViewAutoresizing.FlexibleRightMargin|UIViewAutoresizing.FlexibleBottomMargin|UIViewAutoresizing.FlexibleTopMargin
            if (!indicator.isAnimating ()) || (indicator.hidden)
            {
                indicator.hidden = false
                if (indicator.superview == nil)
                {
                    self.addSubview(indicator)
                }
                indicator.startAnimating()
            }
        }
        image = placeholderImage
        
        kf_setWebURL(URL)
        let task = KingfisherManager.sharedManager.retrieveImageWithURL(URL, optionsInfo: optionsInfo, progressBlock: { (receivedSize, totalSize) -> () in
            if let progressBlock = progressBlock {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    progressBlock(receivedSize: receivedSize, totalSize: totalSize)
                    
                })
            }
            }, completionHandler: {[weak self] (image, error, cacheType, imageURL) -> () in
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    if let sSelf = self where imageURL == sSelf.kf_webURL && image != nil {
                        sSelf.image = image;
                    }