Skip to content

Commit

Permalink
1. Add option named kf_showIndicatorWhenLoading to UIImageView 2. hid…
Browse files Browse the repository at this point in the history
…e indicator when load image finished.
  • Loading branch information
Alex Liu committed Jul 13, 2015
1 parent 1fb4990 commit e0d5929
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Kingfisher-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extension ViewController: UICollectionViewDataSource {

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell

cell.cellImageView.kf_showIndicatorWhenLoading = true
cell.cellImageView.kf_setImageWithURL(NSURL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
println("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
}) { (image, error, cacheType, imageURL) -> () in
Expand Down
43 changes: 30 additions & 13 deletions Kingfisher/UIImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import Foundation
/**
* Set image to use from web.
*/

private var indicator = UIActivityIndicatorView()
public extension UIImageView {

/**
Expand Down Expand Up @@ -105,35 +107,37 @@ import Foundation

: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)

_indicator.center = self.center

_indicator.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin|UIViewAutoresizing.FlexibleRightMargin|UIViewAutoresizing.FlexibleBottomMargin|UIViewAutoresizing.FlexibleTopMargin
if (!_indicator.isAnimating ()) || (_indicator.hidden)
if ( showIndicatorWhenLoading == true)
{
indicator.center = self.center
indicator = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
indicator.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin|UIViewAutoresizing.FlexibleRightMargin|UIViewAutoresizing.FlexibleBottomMargin|UIViewAutoresizing.FlexibleTopMargin
if (!indicator.isAnimating ()) || (indicator.hidden)
{
_indicator.hidden = false
if (_indicator.superview == nil)
indicator.hidden = false
if (indicator.superview == nil)
{
self.addSubview(_indicator)
self.addSubview(indicator)
}
_indicator.startAnimating()
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)
_indicator.removeFromSuperview()
indicator.hidden = true

})
}
Expand All @@ -142,8 +146,10 @@ import Foundation
if (imageURL == self.kf_webURL && image != nil) {
self.image = image;
}
_indicator.removeFromSuperview()

if (showIndicatorWhenLoading == true)
{
indicator.hidden = true
}
completionHandler?(image: image, error: error, cacheType:cacheType, imageURL: imageURL)
})
}
Expand All @@ -154,6 +160,7 @@ import Foundation

// MARK: - Associated Object
private var lastURLkey: Void?
private var showIndicatorWhenLoading: Bool?
public extension UIImageView {
/// Get the image URL binded to this image view.
public var kf_webURL: NSURL? {
Expand All @@ -162,6 +169,16 @@ public extension UIImageView {
}
}

public var kf_showIndicatorWhenLoading: Bool?
{
get{
return showIndicatorWhenLoading
}
set(newVal){
showIndicatorWhenLoading = newVal
}
}

private func kf_setWebURL(URL: NSURL) {
objc_setAssociatedObject(self, &lastURLkey, URL, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
Expand Down

0 comments on commit e0d5929

Please sign in to comment.