Sdwebimage Use--a class library to manage remote picture loadingCategory: Objective-c/iphone program development 2012-05-07 16:32 28955 people read reviews (7) favorite reports Imagemanagerurldownloadcache Storage
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 uses Uiimageview+webcache class (basic application, a category of Uiimageview)
Premise #import Import the Uiimageview+webcache.h file, and then under TableView Cellforrowatindexpath: Method:
#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 U rlwithstring:@ "Http://www.domain.com/path/to/image.jpg"] placeholderimage:[uiimage imagenamed:@ " Placeholder.png "]; Cell.textLabel.text = @ "My text"; return cell;}
Basic code:
[ImageView setimagewithurl:[nsurl urlwithstring: @http://www.domain.com/path/image.jpg]];
For the ios4+ target platform, you can also use the following block statements:
Here we use the new provided Setimagewithurl:method to load the Web Image[cell.imageview setimagewithurl:[nsurl urlwit hstring:@ "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.
Sdwebimagemanager *manager = [Sdwebimagemanager Sharedmanager]; UIImage *cachedimage = [manager Imagewithurl:url]; Load the picture that needs to be cached in if (cachedimage) { ///If the cache hits, use the cached picture directly for action //using 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.
When the download is complete, call the callback method to make the downloaded picture display-(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.
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.
Uiimage*mycachedimage = [[Sdimagecache Sharedimagecache] imagefromkey:mycachekey];
Storing an image to the cache is a way to use Storeimage:forkey:
[[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.
Sdwebimage use--a class library to manage remote picture loading