loading image asynchronously with spinner at the back
I see many apps which have an image frame that loads the image asynchronously and while the image is not loaded yet, it shows either a spinner at the back or a default image. Is there a library for doing that? If yes then what is it? And pl开发者_开发百科ease no three20 (I've had a bad experience with it). If not then how do you create one.
The spinner is called UIActivityIndicatorView
, which is a subclass of UIView, and as such, can be placed anywhere on the screen.
It has startAnimating
and stopAnimating
methods, which begin and end the "spin".
So create the UIActivityIndicatorView, place it with addSubview
where you want, and call startAnimating
(this, as UI activity, has to be done on the main thread). Then call the image loading code asynchronously, and when the image finishes loading, call stopAnimating
on the spinner, put the loaded image on top of it, and then remove the spinner with removeFromSuperview
.
AFAIK, there is no built-in Cocoa API to do this in a call or two (which sounds like what you want).
I personally set up a singleton image cache manager that uses the built-in NSURLConnection
class to load the images, save them to the filesystem, then call delegate methods on the requesting class (which includes a little subclass of UIImageView
) to hand it the loaded image data.
People on SO report success with ASIHTTPRequest
as well, for similar tasks. Worth checking out, I imagine (though I don't use it).
精彩评论