tl; dr
Use - setImageWithURLRequest: placeholderImage: success: failure:
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imgurl] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [cell.detailImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:nil failure:nil];
Image Cache + AFNetworking
Import the UIImageView + AFNetworking and viola header! The UIImageView class now has several methods for loading images, as well as caching them!
In-memory caching:. You can use the following methods for easy caching in memory.
The image will be extracted from the cache in memory, if the image exists, otherwise it will be loaded from the URL and stored in the cache in memory.
Example:
[imageView setImageWithURL:[NSURL URLWithString:imageURL]]; //here, placeholder image is set immediately. [imageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Disk cache You can use the following method if you need to cache an image longer than a user session. (hint: check NSURLRequestCachePolicy in class NSURLRequest )
Note. If a success block is specified, the block must set the image of the image before returning. If no success block is specified, the default behavior is used to set the image using self.image = image .
Example:
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; [imageView setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:nil failure:nil];
source share