Sdwebimage use--a class library to manage remote picture loading
Sdwebimage is hosted on GitHub. Https://github.com/rs/SDWebImage
This class library provides a uiimageview category to support the loading of remote pictures from the network.
Features such as cache management, asynchronous download, same URL download count control, and optimization.
When you add a Sdwebimage class library to your project, you must be aware that you need to add mapkit.framework, because Mkannotationview+webcache.h relies on the framework.
Use the Demo code:
1.UITableView using Uiimageview+webcache Class (Basic application, one category of Uiimageview)
Premise #import Import the Uiimageview+webcache.h file, and then under TableView Cellforrowatindexpath: Method:
[CPP]View Plaincopyprint?
- #import "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;
- }
Basic code:
[CPP]View Plaincopyprint?
- [ImageView setimagewithurl:[nsurl urlwithstring:@<a href="Http://www.domain.com/path/image.jpg" >http:// www.domain.com/path/image.jpg</a>]];
For the ios4+ target platform, you can also use the following block statements:
[CPP]View Plaincopyprint?
- 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"]
- success:^ (UIImage *image) {... success code here ...}
- failure:^ (Nserror *error) {... failure code here ...}];
2. Use the Sdwebimagemanager class: You can do some asynchronous loading work.
[CPP]View Plaincopyprint?
- Sdwebimagemanager *manager = [Sdwebimagemanager Sharedmanager];
- UIImage *cachedimage = [manager Imagewithurl:url]; //Load the pictures that need to be cached in
- if (cachedimage) {
- //If the cache is hit, make a direct use of the cached picture for action
- //Use the cached image immediatly
- } Else {
- //If the cache is not hit, download a picture of the specified network location and give a delegate method
- //Start an async download
- [Manager Downloadwithurl:url Delegate:self];
- }
Of course your class wants to implement the Sdwebimagemanagerdelegate protocol, and to implement the webimagemanager:didfinishwithimage of the Protocol : method.
[CPP]View Plaincopyprint?
- When the download is complete, call the callback method so that the downloaded picture is displayed
- -(void) Webimagemanager: (Sdwebimagemanager *) Imagemanager didfinishwithimage: (UIImage *) Image {
- //Do something with the downloaded image
- }
3. Standalone asynchronous image download
You may use asynchronous image download alone, you must use downloaderwithurl:delegate: To create a Sdwebimagedownloader instance.
[CPP]View Plaincopyprint?
- Downloader =[sdwebimagedownloader Downloaderwithurl:url delegate:self];
This method of Sdwebimagedownloaderdelegate protocol imagedownloader:didfinishwithimage: The download will start immediately and complete when it is invoked.
4. Stand-alone asynchronous image caching
The Sdimagecache class provides an instance of creating an empty cache and uses method Imageforkey: To find the current cache.
[CPP]View Plaincopyprint?
- Uiimage*mycachedimage = [[Sdimagecache Sharedimagecache] imagefromkey:mycachekey];
Storing an image to the cache is a way to use Storeimage:forkey:
[CPP]View Plaincopyprint?
- [[Sdimagecachesharedimagecache] Storeimage:myimage Forkey:mycachekey];
By default, images are stored in the memory cache and in the disk cache. If you want to use StoreImage:forKey:toDisk in memory cache only: The third parameter of the method takes a negative value
To replace.