iOS Web Development (6) Web image loading open source framework Sdwebimage

Source: Internet
Author: User

Sdwebimage is a third-party framework that enables the caching of network images and the processing of such functions.

GitHub's hosting: Https://github.com/rs/SDWebImage


Sdwebimage uses disk caching by default,

In the sandbox/library/cache can find a directory with webimagecache words, you can find the cached picture


Sdwebimage in the form of classification, the Uikit control in the extension of the network picture loading interface, easy to use.

Introduce some of the categories in Sdwebimage:

Uiimageview Loading Network pictures

UIButton Loading Network pictures

uiimage Display GIF pictures

And the monitoring of the progress of the picture download, as well as a brief description of the implementation process within the framework



    • Uiimageview+webcache classification

Provides a number of ways to load pictures from the network, and these operations are all asynchronous

-(void) Sd_setimagewithurl: (Nsurl *) url-(void) Sd_setimagewithurl: (nsurl *) URL completed: (sdwebimagecompletionblock ) Completedblock

Some of the methods also provide a placeholder picture of the operation

-(void) Sd_setimagewithurl: (nsurl *) URL placeholderimage: (UIImage *) placeholder-(void) Sd_setimagewithurl: (Nsurl *) URL placeholderimage: (UIImage *) placeholder completed: (sdwebimagecompletionblock) Completedblock

Some of the methods also provide additional options for specifying the policy for caching

-(void) Sd_setimagewithurl: (nsurl *) URL placeholderimage: (UIImage *) placeholder options: (sdwebimageoptions) options- (void) Sd_setimagewithurl: (nsurl *) URL placeholderimage: (UIImage *) placeholder options: (sdwebimageoptions) options Completed: (sdwebimagecompletionblock) Completedblock

Such as:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7C/B3/wKioL1bWkGyghYH5AAGsIiQrXpI720.png "title=" screen shot 2016-03-02 pm 3.01.43.png "width=" 650 "height=" 272 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:650px;height:272px; " alt= "Wkiol1bwkgyghyh5aagsiiqrxpi720.png"/>



    • Uibutton+webcache classification

Similar to Uiimageview+webcache, but can display two images on UIButton

Loading of background images

-  (void) Sd_setbackgroundimagewithurl: (nsurl *) Url forstate: (uicontrolstate) state-  (void) SD _setbackgroundimagewithurl: (nsurl *) Url forstate: (uicontrolstate) state completed: ( Sdwebimagecompletionblock) completedblock-  (void) Sd_setbackgroundimagewithurl: (nsurl *) url  Forstate: (uicontrolstate) State placeholderimage: (uiimage *) placeholder-  (void) sd_ Setbackgroundimagewithurl: (nsurl *) Url forstate: (uicontrolstate) State placeholderimage: (UIImage  *) placeholder completed: (sdwebimagecompletionblock) completedblock-  (void) sd_ Setbackgroundimagewithurl: (nsurl *) Url forstate: (uicontrolstate) State placeholderimage: (UIImage  *) Placeholder options: (sdwebimageoptions) options-  (void) Sd_setbackgroundimagewithurl: (NSURL  *) Url forstate: (uicontrolstate) State placeholderimage: (uiimage *) placeholder options :(sdwebimageoptions) options completed: (sdwebimagecompletioNblock) Completedblock 

Loading of pictures

-(void) Sd_setimagewithurl: (nsurl *) URL forstate: (uicontrolstate) state-(void) Sd_setimagewithurl: (nsurl *) URL Forstate: (uicontrolstate) statecompleted: (sdwebimagecompletionblock) completedblock-(void) Sd_setImageWithURL: ( Nsurl *) URL forstate: (uicontrolstate) Stateplaceholderimage: (UIImage *) placeholder-(void) Sd_setimagewithurl: (NSURL *) URL forstate: (uicontrolstate) Stateplaceholderimage: (UIImage *) placeholder completed: (sdwebimagecompletionblock) completedblock-(void) Sd_setimagewithurl: (nsurl *) URL forstate: (uicontrolstate) Stateplaceholderimage: (UIImage *) Placeholder options: (sdwebimageoptions) options-(void) Sd_setimagewithurl: (nsurl *) URL forstate: (uicontrolstate) Stateplaceholderimage: (UIImage *) placeholder options: (sdwebimageoptions) options completed: ( Sdwebimagecompletionblock) Completedblock


    • Uiimage+gif categories for loading GIF images

This classification provides a way to create a GIF image object

+ (UIImage *) sd_animatedgifnamed: (NSString *) name+ (UIImage *) Sd_animatedgifwithdata: (NSData *) data

Interested friends, you can look at the implementation code of these two methods in the framework


    • Monitoring of picture Download progress

In many apps, images load with a progress bar loaded, and Uiimageview also provides a way to continuously get the picture's download progress:

-(void) Sd_setimagewithurl: (nsurl *) URL placeholderimage: (UIImage *) placeholder options: (sdwebimageoptions) options Progress: (Sdwebimagedownloaderprogressblock) Progressblock completed: (sdwebimagecompletionblock) CompletedBlock

Such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/B4/wKioL1bWkmfhs2USAAFfZ_IzU-E627.png "title=" screen shot 2016-03-02 pm 3.10.38.png "width=" 649 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:649px;height:170px; " alt= "Wkiol1bwkmfhs2usaaffz_izu-e627.png"/>


    • Analysis of the principle of sdwebimage

Sdwebimage supports asynchronous image download + cache, loading the basic flow of pictures:

    1. Show Placeholderimage,sdwebimagemanager start processing pictures based on URL

    2. Finds whether a picture has been downloaded from the in-memory cache, and if found, the callback displays the picture

    3. If not, the disk cache is looked up in the child thread, and if found, the callback displays the picture

    4. If not, start downloading the picture from the URL (using a Sdwebimagedownloader Downloader object)

    5. By proxy mode, after the image is downloaded, the image is decoded and the callback displays the image.

    6. Save the picture to Sdimagecache, the memory cache and the disk cache are saved at the same time, and the write disk operation will be executed in the child thread.


Sdimagecache will register some message notifications at initialization time

Cleans up the memory cache when a memory warning or fallback to the background

Clean out outdated pictures when the program exits


The purpose of the image decoding (space-time change)

UIImage Imagewithdata: Each time the data is extracted into an image, that is, each display will have a decompression operation

Sdwebimagedecoder is to first extract the data resources on a picture, display no longer need to unzip



This article is from the "Ammon" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746756

iOS Web Development (6) Web image loading open source framework 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.