IOS Sdwebimage Implementation principle

Source: Internet
Author: User

Sdwebimage
This class library provides a Uiimageview
Category to support loading remote pictures from the network. Features such as cache management, asynchronous download, same URL download count control, and optimization.
Sdwebimage supports asynchronous picture downloads + caches, providing Uiimageview+webcacha
Category, easy to use. Sdwebimage the process of loading pictures:
1. Entrance SetImageWithURL:placeholderImage:options: will first put Placeholderimage
Display, and then Sdwebimagemanager starts processing the picture based on the URL.
2. Enter Sdwebimagemanager-downloadwithurl:delegate:options:userinfo:, give
Sdimagecache whether a picture has been downloaded from the cache
QueryDiskCacheForKey:delegate: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 not in the memory cache, generate Nsinvocationoperation
Added to the queue to start looking for pictures from the hard disk is already cached.
6. Try to read the picture file according to the Urlkey in the hard disk cache directory. This step is in the Nsoperation
The operation is performed, so the callback notifydelegate the result back to the main thread:.
7. If the previous action reads a picture from the hard disk, add the picture 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.
Need to download picture, callback ImageCache:didNotFindImageForKey:userInfo:.
9. Share or regenerate a downloader sdwebimagedownloader start downloading pictures.
10. Image download by nsurlconnection to achieve the relevant delegate
To determine the image download, download complete and download failed.
Connection:didreceivedata: The use of ImageIO to download the progress by the picture loading effect.

Connectiondidfinishloading: When the data download is complete, give it to Sdwebimagedecoder.
Do picture decoding processing.
13. Image decoding processing completed in one nsoperationqueue, 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. On the main thread notifydelegateonmainthreadwithinfo:
The completion of the decoding, ImageDecoder:didFinishDecodingImage:userInfo: callback to
Sdwebimagedownloader.
Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager
Let 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 simultaneously. Writing files to the hard disk is also done in a separate nsinvocationoperation to avoid slowing down the main thread.
Sdimagecache will register some message notifications at the time of initialization,
Clean up the memory image cache when the memory is warning or back to the background, and clean out outdated images when the app ends.
SDWI also offers Uibutton+webcache and Mkannotationview+webcache.
, easy to use.
Sdwebimageprefetcher can pre-download images for later use

Where to use the management class:
The most common use of this library is the category of Uiimageview:
Uiimageview (Webcache).
One of the most common ways to do this is to load a picture of the network according to the URL. It is implemented as follows:
-(void) Setimagewithurl: (nsurl *) URL placeholderimage: (uiimage*) placeholder

{

Sdwebimagemanager *manager = [Sdwebimagemanagersharedmanager];

Remove in progress downloader from queue

[Manager Cancelfordelegate:self];

Self.image = placeholder;

if (URL)

{

[Manager Downloadwithurl:url Delegate:self];

}

}
The biggest benefit of this approach is that you can add the network download function directly without changing the UI's class.
Stand-alone 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];
So Sdwebimagedownloaderdelegate
Protocol method Imagedownloader:didfinishwithimage: The download will start immediately and complete when it is called.
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:
[[Sdimagecache Sharedimagecache] 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.
The role of the Sdwebimage library:
The task of asynchronously loading a replacement picture is achieved by extending the category of Uiimageview.
The main objects to use:
1, Uiimageview (webcache) category, ingress encapsulation, implementation of reading the image after the completion of the callback
2, Sdwebimagemanager, the management of the picture Transfer station, record those pictures are reading.
Read cache to downlevel (call Sdimagecache
), or read the object to the network (call Sdwebimagedownloader).
Implement callbacks for Sdimagecache and Sdwebimagedownloader.
3, Sdimagecache, according to the URL of the MD5
Abstract store and read images (implementations exist in memory or on hard disk implementations)
Implement picture and memory cleanup work.
4, Sdwebimagedownloader, according to the URL
Read data to the network (two ways of enabling partial and full read and then notifying callbacks)
What does Sdimagecache do with data management?
Sdimagecache is divided into two parts, one is the memory level, the other is the hard disk level.
The memory plane is quite a cache, to Key-value
stored in the form of a picture. All cached images are cleared when there is not enough memory.
In the way of searching the file system to do management, the file replacement method is in the time unit,
Image files with a rejection time greater than one week.
When Sdwebimagemanager to Sdimagecache
To the resource, first search the memory level of data, if there is a direct return, there is no way to access the disk, the picture is read from the disk, and then do decoder
, put the picture object on the memory plane for backup, and then return to the call layer

IOS Sdwebimage Implementation principle

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.