Roduck Nickes
Roduck Nickes

Reputation: 1021

Use activityIndicator as image placeholder

I use UIImageView+AFNetworking.swift in my project, and to load image to imageView with placeholder, i use this code:

imageCell!.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "PlaceholderIMG"))

How can i replace the PlaceholderIMG with a activityIndicator instead?

Upvotes: 0

Views: 1701

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12842

Use setImageWithURLRequest from UIImageView+AFNetworking.swift instead of your solution. So here's a rough example:

// create a UIActivityIndicatorView with the 
let ai = UIActivityIndicatorView(frame: cell.imageView.frame)
// Add the UIActivityIndicatorView as a subview on the cell
cell.addSubview(ai)
// Start the UIActivityIndicatorView animating
ai.startAnimating()
// Load the remote image with this different API, and send nil as the placeholder
cell.imageView?.setImageWithURLRequest(NSURLRequest(URL: NSURL(string: post.image)),
    placeholderImage: nil,
    success: { (request, response, image) -> Void in
        // on completion, stop it and remove it
        ai.stopAnimating()
        ai.removeFromSuperview()
        cell.imageView?.image = image
    },
    failure: { (request, response, error) -> Void in
        // on completion, stop it and remove it
        ai.stopAnimating()
        ai.removeFromSuperview()
})

Upvotes: 1

Related Questions