AFNetworking is one of the most popular open-source networking frameworks for iOS. You're probably already using it, but if you're not, you should consider it. One of the lesser-known features of AFNetworking is the ability to easily load an image from a URL to be used in a
Start by importing the
UIImageView+AFNetworkingheader and several useful methods will be added to the
For simple in-memory caching, you can use the
setImageWithURL:placeholderImage:methods and the image will be retrieved from the in-memory cache, if the image exists, otherwise it will be fetched from the URL and stored in the cache.
[imageView setImageWithURL:[NSURL URLWithString:imageURL]]; // Or, with a placeholder image displayed while the image is being downloaded: [imageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"placeholder"]];
If you need to cache the image for longer than the user's session, you can use the
setImageWithURLRequest:placeholderImage:success:failure:method. Set the caching policy on your
NSURLRequestto whatever you need and you're good to go!
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [imageView setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:@"placeholder"] success:nil failure:nil];
Note that if you supply a success block, you will be responsible for setting the image within the success block. If one is not provided, AFNetworking will set the image for you.
If AFNetworking isn't your thing, I highly recommend using the excellent SDWebImage instead.