Web image (Network images)
The library provides a uiimageview that supports remote images from the Web category
It provides:
- Add network images and cache management to the Uiimageview category of the Cocoa Touch framework
- Asynchronous image Download
- An asynchronous memory + disk image caching with automatic cache expiration handling
- Support GIF animations
- Support WEBP Format
- Background image Decompression
- Ensure that the same URL is not downloaded more than once
- Make sure the spoofed URL doesn't try to download it over and over again
- Ensure that the main thread is never blocked
- performances!
- Using GCD and Arc
Note: Sdwebimage 3.0 is not backwards compatible with 2.0 and requires a minimum version of iOS 5.0. If your iOS version is below 5.0, please use version 2.0.
How to use
API document Address http://hackemist.com/SDWebImage/doc/.
Using uiimageview+webcache category with UITableView
Only need to introduce uiimageview+webcache.h header file, in Uitableviewdatasource method Tableview:cellforrowatindexpath: Call the Setimagewithurl:placeholderimage: method. From asynchronous download to cache management, everything will work for you.
#import <SDWebImage/UIImageView+WebCache.h>...- (UITableViewCell *)TableView:(UITableView *)TableView Cellforrowatindexpath:(Nsindexpath *)Indexpath{ Static NSString *Myidentifier = @ "Myidentifier"; UITableViewCell *Cell = [TableView Dequeuereusablecellwithidentifier:Myidentifier]; if (Cell == Nil) { Cell = [[[UITableViewCell Alloc] Initwithstyle:Uitableviewcellstyledefault Reuseidentifier:Myidentifier] autorelease]; } //Here we use the new provided Setimagewithurl:method to load the Web image [Cell.ImageView Setimagewithurl:[Nsurl urlwithstring:@ "Http://www.domain.com/path/to/image.jpg"] Placeholderimage:[UIImage imagenamed:@ "Placeholder.png"]]; Cell.Textlabel.text = @ "My Text"; return Cell;}
Using blocks
with blocks, you will be informed of the download progress, success or failure when completed:
//Here we use the new provided Setimagewithurl:method to load the Web image[Cell.ImageView Setimagewithurl:[Nsurl urlwithstring:@ "Http://www.domain.com/path/to/image.jpg"] Placeholderimage:[UIImage imagenamed:@ "Placeholder.png"] Completed:^(UIImage *Image, Nserror *Error, Sdimagecachetype CacheType) {... Completion Code Here ...}];Note: If the request is canceled, then the block will not be invoked (either successfully or unsuccessfully). Using SdwebimagemanagerThe Sdwebimagemanager is the class behind the Uiimageview+webcache category. It ties the asynchronous downloader with the image cache store. You can use the this class directly to benefit from web image downloading with caching in another context than a UIView (ie:w ITH Cocoa)here's how to use the Sdwebimagemanager code:
Sdwebimagemanager *Manager = [Sdwebimagemanager Sharedmanager];[Manager Downloadwithurl:ImageURL Options:0 Progress:^(Nsuinteger receivedsize, Long Long expectedsize) { //Progression tracking code } Completed:^(UIImage *Image, Nserror *Error, Sdimagecachetype CacheType) { if (Image) { //Do something with image } }];Using Asynchronous Image Downloader independentlycan also be used to download asynchronous images independently:
[Sdwebimagedownloader.Shareddownloader Downloadimagewithurl:ImageURL Options:0 Progress:^(Nsuinteger receivedsize, Long Long expectedsize) { //Progression tracking code } Completed:^(UIImage *Image, NSData *Data, Nserror *Error, BOOL finished) { if (Image && finished) { //Do something with image } }];Using Asynchronous Image Caching independentlyIt is also possible to use asynchronous-based image cache storage independently. Sdimagecache maintains a memory cache and an optional disk cache. Disk High the write operation for the fast cache isAsynchronouscarried on, so it would not giveUIincrease the unnecessary delay. for convenience, the Sdimagecache class provides a singleton, but you can also create a new instance if you want to create a separate cache namespace.
You can use the Imageforkey: method to find the cache, and if it returns nil, the current image does not own the cache. So you are responsible for generating and caching it. The cache key is a unique identifier for the image cache in a program, which is usually the URL of the image.
Sdimagecache * Imagecache = [sdimagecache. Alloc Initwithnamespace : @ "MyNamespace" ]; [imagecachequerydiskcacheforkey:mycachekeydone : ^(UIImage*image){ //image is not nil if image was found }];
by default, if an image cannot be found in the memory cache, Sdimagecache will look for the cache. You can call the alternative method Imagefrommemorycacheforkey: To prevent this from happening. to store an image into the cache, you can use the Storeimage:forkey: Method:
[[sdimagecachesharedimagecache]storeimage:myimage forkey:mycachekey];
By default, images are stored in memory cache and cached on disk (asynchronously). If you want to cache only in memory, use the workaround StoreImage:forKey:toDisk:, the third parameter is a negative number.
Using the cache key filterSometimes you may not want to use the image URL as the cache key because the URL may be dynamic (i.e.: for access control purpose). Sdwebimagemanager provides a to set a cache key filter that takes the Nsurl as input, and output a cache key NSString ( This sentence is not very understanding).
The following example in the application's delegate sets a filter that removes any query string from the URL before using its cache key ( The following example sets a filter in the application delegate that would remove any query-string from the URL before To use it as a cache key):
- (BOOL)Application:(uiapplication *)Application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions{ Sdwebimagemanager.Sharedmanager.Cachekeyfilter:^(Nsurl *URL) { URL = [[[Nsurl Alloc] Initwithscheme:URL.Scheme Host:URL.Host Path:URL.Path] autorelease]; return [URL absolutestring]; }; //Your app init code ... return YES;}Using dynamic image size with UITableViewCellUITableView determines the size of an image by the image set by the first cell. If your remote image does not have the same size as the image placeholder, you may experience strange deformation scaling issues. The following article gives a way to solve this problem:
http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/
Handle Image Refresh (to control images refreshed)By default, Sdwebimage is indeed a very aggressive cache. it ignores all types of cache control headers that are returned through the HTTP server and and there is no time limit to cache the returned image. This means that your image URL is never changed, pointing to the static URL of the image. If you point to a picture change, the URL will change accordingly.
If you do not control your image server, you cannot change its URL when its content is updated. The Facebook avatar is an example of this situation. in this case, you can use the sdwebimagerefreshcached flag. This will slightly degrade performance, but will take into account the HTTP cache control header:
[imageViewsetimagewithurl:[nsurlurlwithstring : @ "Https://graph.facebook.com/olivier.poitrey/picture" ] placeholderimage:[UIImageimagenamed: @ "Avatar-placeholder.png" ] options:sdwebimagerefreshcached];
Add a progress indicator (adding a progress indicator)View this category: https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage
installation (Installation)There are two ways of using sdwebimage: 1. Drag all the files into the project; 2. Drag the project into the project as a static library.
ADD The Sdwebimage project to your project
- Download and unzip the last version of the framework from the Download page
- Right-click on the Project Navigator and select "Add Files to" Your project ":
- In the dialog, select Sdwebimage.framework:
- Check the "Copy items into destination group ' s folder (if needed)" checkbox
add dependencies (adding dependencies)
- In you application project app's target settings, find the "Build Phases" section and open the "Link Binary with Libraries "Block:
- Click the "+" button again and select the "Imageio.framework" ( also missing a "mapkit.framework", not mentioned in the Readme ), this is needed By the Progressive download feature:
Add Linker flag (add link flag)
Open the ' Build Settings ' tab, in the ' Linking ' section, locate the ' other Linker Flags ' setting and add the '-OBJC ' flag:
Import headers in your source files (import header files into your resource file)Import the following header file in the resource file that you need to use for this library:
#import <SDWebImage/UIImageView+WebCache.h>
build project (Compile project ) There should be no mistake in your project here. If so, go to Google and find it.
Future enhancements (next enhancements)LRU Memory Cache cleanup instead of reset on memory warning