iOS picture loading-sdwebimage

Source: Internet
Author: User

First, sdwebimage internal realization process

1, entrance setImageWithURL:placeholderImage:options: will display the Placeholderimage first, and then Sdwebimagemanager start processing the picture according to the URL.

2, enter Sdwebimagemanager-downloadwithurl:delegate:options:userinfo:, to Sdimagecache from the cache to find whether the picture has been downloaded QueryDiskCacheForKey:delegate:userInfo:.

3, first from the memory image 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, according to Urlkey in the hard disk cache directory to try to read the picture file. This step is performed at Nsoperation, so the callback notifydelegate is returned to the main thread:.

7, if the previous operation read from the hard disk to the picture, the picture is added to the memory cache (if the free memory is too small, the memory cache will be 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 the caches, need to download the picture, 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.

One by one, Connection:didreceivedata: In the use of ImageIO made by the picture download progress loading effect.

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

13, picture decoding processing is done in a 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, in the main thread Notifydelegateonmainthreadwithinfo: the completion of the declaration of decoding, ImageDecoder:didFinishDecodingImage:userInfo: callback to Sdwebimagedownloader.

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

16, notify all the Downloaddelegates download complete, callback to the desired place to display pictures.

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.

At the time of initialization, Sdimagecache will register some message notifications, 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 for ease of use.

Sdwebimageprefetcher can pre-download images for easy follow-up.

Second, 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 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 interface will call this interface, this is the practice of most frameworks.

2, get sdwebimage disk cache size, in the project sometimes need to count the application's disk cache content size, then get the image cache size is to use this interface to implement

[Sdimagecache Sharedimagecache] getsize];

3. Clean up the memory cache, clean up the cached picture resource in memory, and free up memory resources.

[[Sdimagecache Sharedimagecache] clearmemory];

4, with the clean-up memory cache, there is also the interface to clean up the disk cache

[[Sdimagecache Sharedimagecache] cleardisk];

iOS picture loading-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.