Use of the latest version of Sdwebimage

Source: Internet
Author: User

The first step is to download the Sdwebimage and import the project. GitHub Escrow Address Https://github.com/rs/SDWebImage

Step two, import the header files where needed

1 #import "UIImageView+WebCache.h"

The third step, call Sd_setimagewithurl: Method cache The picture, note that this is the new version of the new method, the old method is Setimagewithurl:. Here are a few ways to introduce them.

1. Sd_setimagewithurl:

12 //图片缓存的基本代码,就是这么简单    [self.image1 sd_setImageWithURL:imagePath1];

2. Sd_setimagewithurl:completed:

123456 < Code class= "JS comments" >//with block  can do something after the picture is loaded      [self.image2 sd_setimagewithurl:imagepath2 completed:^ (UIImage * Image, nserror *error, sdimagecachetype cachetype, nsurl *imageurl)  { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; nslog (@ "Here you can do something after the picture is loaded" ); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;        }];

3. Sd_setimagewithurl:placeholderimage:

12 //给一张默认图片,先使用默认图片,当图片加载完成后再替换    [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];

4. Sd_setImageWithURL:placeholderImage:completed:

123456 //Use the default picture, and block  Do something after you're done &NBSP;&NBSP;&NBSP;&NBSP; [self.image1  sd_setimagewithurl:imagepath1 placeholderimage:[uiimage imagenamed:@ "default" ] completed:^ (uiimage *image, nserror *error, sdimagecachetype  cachetype, nsurl *imageurl)  {                     nslog (@ "things to do after the picture is loaded" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;        }];

5. Sd_setImageWithURL:placeholderImage:options:

123 //options 选择方式        [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"] options:SDWebImageRetryFailed];

Others are not introduced, OC is a document language, look at the method name to know what to do. In addition to the options option, the other method is integrated storage, that is, memory cache and disk cache in combination, if you only need memory cache, then the options here to choose Sdwebimagecachememoryonly.

If you don't want to know more about it, you can already use Sdwebimage to cache the image, and then I'll explain all options and the sdwebimage internal execution process.

One, options all option:

123456789101112131415161718192021222324252627282930313233   //失败后重试     SDWebImageRetryFailed = 1 << 0,          //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。     SDWebImageLowPriority = 1 << 1,          //只进行内存缓存     SDWebImageCacheMemoryOnly = 1 << 2,          //这个标志可以渐进式下载,显示的图像是逐步在下载     SDWebImageProgressiveDownload = 1 << 3,          //刷新缓存     SDWebImageRefreshCached = 1 << 4,          //后台下载     SDWebImageContinueInBackground = 1 << 5,          //NSMutableURLRequest.HTTPShouldHandleCookies = YES;          SDWebImageHandleCookies = 1 << 6,          //允许使用无效的SSL证书     //SDWebImageAllowInvalidSSLCertificates = 1 << 7,          //优先下载     SDWebImageHighPriority = 1 << 8,           //延迟占位符     SDWebImageDelayPlaceholder = 1 << 9,          //改变动画形象     SDWebImageTransformAnimatedImage = 1 << 10,

Second, sdwebimage internal realization process

  1. Entry SetImageWithURL:placeholderImage:options: The Placeholderimage is displayed first, and then the Sdwebimagemanager is processed based on the URL.

  2. Enter Sdwebimagemanager-downloadwithurl:delegate:options:userinfo:, to Sdimagecache from the cache to find whether the picture has been downloaded Querydiskcacheforkey :d Elegate:userinfo:.

  3. First from the memory picture cache to find if there is a picture, if there is already a picture cache in memory, Sdimagecachedelegate callback ImageCache:didFindImage:forKey:userInfo: to Sdwebimagemanager.

  4. Sdwebimagemanagerdelegate callback Webimagemanager:didfinishwithimage: to Uiimageview+webcache and other front-end display pictures.

  5. If no memory is in the cache, the build nsinvocationoperation is added to the queue to start looking for pictures from the hard disk if it is already cached.

  6. Try to read the picture file according to Urlkey in the hard disk cache directory. This step is performed at Nsoperation, so the callback notifydelegate is returned to the main thread:.

  7. If the previous action reads a picture from the hard disk, the picture is added to the in-memory cache (if the free memory is too small, the memory cache is emptied first). Sdimagecachedelegate callback ImageCache:didFindImage:forKey:userInfo:. And then callback the display image.

  8. If the picture is not read from the hard disk cache directory, the picture is not present in all caches, the picture needs to be downloaded, and the callback ImageCache:didNotFindImageForKey:userInfo:.

  9. Share or regenerate a downloader sdwebimagedownloader start downloading pictures.

  10. Image download by nsurlconnection to do, to achieve the relevant delegate to determine the picture download, download complete and download failed.

  11. Connection:didreceivedata: The use of ImageIO to download the progress by the picture loading effect.

  12. Connectiondidfinishloading: After the data download is finished, give sdwebimagedecoder to do the image decoding processing.

  13. The image decoding process is done in a nsoperationqueue and does not slow down the main thread UI. If there is a need to download the image two times, it is best to complete here, the efficiency will be much better.

  14. In the main thread Notifydelegateonmainthreadwithinfo: announce the decoding complete, ImageDecoder:didFinishDecodingImage:userInfo: callback to Sdwebimagedownloader.

  15. Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager tell the picture download complete.

  16. Notify all downloaddelegates download complete, callback to show the image where needed.

  17. Save the picture to Sdimagecache, the memory cache and the hard disk cache are saved at the same time. Writing files to the hard disk is also done in a separate nsinvocationoperation to avoid slowing down the main thread.

  18. Sdimagecache registers some message notifications at initialization time, cleans up the memory image cache when the memory is warning or back to the background, and cleans up outdated images when the app is finished.

  19. SDWI also offers uibutton+webcache and mkannotationview+webcache for ease of use.

  20. Sdwebimageprefetcher can pre-download images for later use.

As you can see from the above process, when you call Setimagewithurl: method, he will automatically go to do so many things for you, when you need to do something at a specific time, you can override these methods. For example, in the process of downloading a picture to respond to an event, this method is overwritten:

1234567891011 //覆盖方法,指哪打哪,这个方法是下载imagePath2的时候响应    SDWebImageManager *manager = [SDWebImageManager sharedManager];        [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {                 NSLog(@"显示当前进度");            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {                NSLog(@"下载完成");    }];

For beginners, a good picture cache can be achieved with several methods of Sd_setimagewithurl:

Transferred from: http://www.cocoachina.com/ios/20141212/10622.html

Use of the latest version of Sdwebimage

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.