iOS third-party class library-sdwebimage use

Source: Internet
Author: User

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?
  1. #import "Uiimageview+webcache.h"
  2. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
  3. {
  4. static NSString *myidentifier = @"Myidentifier";
  5. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:myidentifier];
  6. if (cell = = nil)
  7. {
  8. cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault
  9. Reuseidentifier:myidentifier] autorelease];
  10. }
  11. //Here we use the new provided Setimagewithurl:method to load the Web image
  12. [Cell.imageview setimagewithurl:[nsurl urlwithstring:@"http://www.domain.com/path/to/image.jpg"]
  13. Placeholderimage:[uiimage imagenamed:@"Placeholder.png"];
  14. Cell.textLabel.text = @"My text";
  15. return cell;
  16. }

Basic code:

[CPP]View Plaincopyprint?
    1. [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?
  1. Here we use the new provided Setimagewithurl:method to load the Web image
  2. [Cell.imageview setimagewithurl:[nsurl urlwithstring:@"http://www.domain.com/path/to/image.jpg"]
  3. Placeholderimage:[uiimage imagenamed:@"Placeholder.png"]
  4. success:^ (UIImage *image) {... success code here ...}
  5. failure:^ (Nserror *error) {... failure code here ...}];

2. Use the Sdwebimagemanager class: You can do some asynchronous loading work.

[CPP]View Plaincopyprint?
  1. Sdwebimagemanager *manager = [Sdwebimagemanager Sharedmanager];
  2. UIImage *cachedimage = [manager Imagewithurl:url]; //Load the pictures that need to be cached in
  3. if (cachedimage) {
  4. //If the cache is hit, make a direct use of the cached picture for action
  5. //Use the cached image immediatly
  6. } Else {
  7. //If the cache is not hit, download a picture of the specified network location and give a delegate method
  8. //Start an async download
  9. [Manager Downloadwithurl:url Delegate:self];
  10. }

Of course your class wants to implement the Sdwebimagemanagerdelegate protocol, and to implement the webimagemanager:didfinishwithimage of the Protocol : method.

[CPP]View Plaincopyprint?
    1. When the download is complete, call the callback method so that the downloaded picture is displayed
    2. -(void) Webimagemanager: (Sdwebimagemanager *) Imagemanager didfinishwithimage: (UIImage *) Image {
    3. //Do something with the downloaded image
    4. }

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?
    1. 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?
    1. Uiimage*mycachedimage = [[Sdimagecache Sharedimagecache] imagefromkey:mycachekey];

Storing an image to the cache is a way to use Storeimage:forkey:

[CPP]View Plaincopyprint?
    1. [[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.