iOS picture loading frame-sdwebimage interpretation

Source: Internet
Author: User

In iOS's picture-loading frame,sdwebimage is a big half. It supports downloading and caching pictures from the network and setting the picture to the corresponding Uiimageview control or UIButton control. Using Sdwebimage in your project to manage picture loading related operations can greatly improve development efficiency and allow us to focus more on business logic implementations.

Introduction to Sdwebimage

1. Provides a uiimageview category for loading network images and managing the caching of network images
2. Use asynchronous mode to download network pictures
3. Asynchronously uses Memory+disk to cache network pictures and automatically manage caches.
4. Support GIF animation
5. Support WEBP Format
6. Web images of the same URL will not be downloaded repeatedly
7. Invalid URL will not be retried indefinitely
8. Time-consuming operations are on child threads, ensuring that the main thread is not blocked
9. Using GCD and Arc
10. Support Arm64

Sdwebimage use

1. Use the Iimageview+webcache category to load a picture of the cell in the UITableView

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

2. Using blocks, this scheme can be used in the network picture loading process to know the picture download progress and picture loading success or not

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {    //... completion code here ...  }];

3. Use Sdwebimagemanager,sdwebimagemanager to provide an interface for the implementation of the Uiimageview+webcache category.

SDWebImageManager *manager = [SDWebImageManager sharedManager] ;[manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger   receivedSize, NSInteger expectedSize) {       // progression tracking code }  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,   BOOL finished, NSURL *imageURL) {    if (image) {     // do something with image   } }];

4. Loading pictures also use Sdwebimagedownloader and Sdimagecache, but that's not something we often use. The basic 3 methods mentioned above can meet the requirements.

Sdwebimage process
Uiimageview picture loading Process Sdwebimage interface

Sdwebimage is a mature and relatively large framework, but the use of the process does not require too many interfaces, which is a kind of code encapsulation degree of embodiment. Here are a few more commonly used interfaces.

    1. To Uiimageview to set the interface of the picture, Sdwebimage has to provide more than one interface to the Uiimageview settings picture, eventually all the interfaces will call this interface, this is the practice of most frameworks.


      Interface for setting up a picture for Uiimageview
    2. Get the disk cache size of sdwebimage, in the project sometimes need to count the size of the disk cache content of the application, then the cache size of the image is to use this interface to implement

      [SDImageCache sharedImageCache] getSize];
    3. Cleans up the memory cache, cleans up cached picture resources in memory, and frees up memory resources.

      [[SDImageCache sharedImageCache] clearMemory];
    4. With the clean-up memory cache, there is also an interface to clean up the disk cache.

      [[SDImageCache sharedImageCache] clearDisk];
Sdwebimage parsing

The analysis mainly revolves around the picture loading flow of sdwebimage, introduces some processing methods and design ideas in the process of loading picture of sdwebimage frame.

    1. Set the picture to Uiimageview, and then sdwebimage call this final picture loading method.


      1 setting a picture for Uiimageview
    2. Before starting to load the image first cancels the corresponding Uiimageview previous picture download operation. Imagine, if we set a new picture for Uiimageview, then we will also care about the Uiimageview which image was previously loaded? Should not care about it! That should try to uiimageview the previous loading picture related operations to cancel out.

      [self sd_cancelCurrentImageLoad]


      2 Cancel the corresponding Uiimageview previous picture download operation

This method passes the turnover, finally calls the following method, the framework puts the picture corresponding download operation in UIView's One custom dictionary property (operationdictionary), cancels the download operation the first step is also from this UIView custom dictionary property ( Operationdictionary), remove all the download operations, then call the Cancel method, and finally remove the canceled operation from the (Operationdictionary) dictionary property.


The final cancellation download method

3. Remove the previously useless picture download operation and create a new picture download operation and set it to a custom dictionary property (operationdictionary) in UIView.


3 Create a new picture download operation

4. See how to create a new image download operation, the framework holds a list of invalid URLs, if the URL is invalid, it will be added to this list, ensure that the URL is not repeated requests failed.


4 Picture Download operation

Generates a unique key based on the given URL and then uses the key to find the corresponding image cache in the cache.


Find a picture cache

5. Read the image cache, according to key first read the image cache from memory, if not hit the memory cache read the disk cache, if the disk cache hit, then read the disk cache into memory as memory cache. If none of the cache is hit, start downloading the image in the doneblock that is executed.


5 reading the picture cache

6. When the image download operation is complete, the data corresponding to the image will be recalled via completed block.


6 Picture Download operation

In the image download method, a method is called to add various block callbacks during the creation and download process.


Image Download method

Add the status of the URL loading process callback block


Status Callback Block

If the URL is loaded for the first time, then the Createcallback callback block is executed, then the network request is started in the Createcallback, and the various progress block callbacks are executed during the download process.


Building Network Requests

7. When the picture is downloaded, it will return to the done block callback for image conversion processing and cache operation.


7 Picture conversion processing and caching operations

Back to the Uiimageview control's settings picture method block callback, the corresponding Uiimageview set the picture, the operation process to complete this.


Set up picture summary in block

Sdwebimage as a good picture loading framework, the use of methods and interfaces provided to developers is very friendly. The internal implementation is mostly a block way to implement callbacks, code reading may not be so intuitive. This article is intended to give you an overview of the Sdwebimage framework of the picture loading process, where the details are limited to the length of the detailed. My ability is limited, the article inevitably error, if you have found in the process of reading unreasonable or wrong place in the comments, I will be in the first time to amend, greatly appreciated .

If this article to you played a certain role, please at the bottom of a praise , let me know this article has played its role, but also for my hard code word encouragement it, haha!

==========================================
A friend in the comments wanted to know about the treatment of 304, which I added here.

Sdwebimage in the Load Picture network request Nsurlconnection Agent in the Httpcode made a judgment, when the Httpcode for 304 to abandon the download, read the cache.


304 Processing


To work, bin.
Links: Http://www.jianshu.com/p/be9a0a088feb
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

iOS picture loading frame-sdwebimage interpretation

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.