Sdwebimage Source code Interpretation (iv) Uiimageview+webcache

Source: Internet
Author: User

Tag: ext nload requires INI ade nal return cancel ring

Objective

These three categories are relatively simple, and they are interpreted together.

Body Uiimageview+webcache

This is the classification of Uiimageview, which is designed to load images using Uiimageview.

Let's look at the specific code

1- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL;2 3- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL4Placeholderimage: (Nullable UIImage *) placeholder;5 6- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL7Placeholderimage: (Nullable UIImage *) Placeholder8 options: (sdwebimageoptions) options;9 Ten- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL One Completed: (nullable sdexternalcompletionblock) Completedblock; A  -- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL -Placeholderimage: (Nullable UIImage *) Placeholder the Completed: (nullable sdexternalcompletionblock) Completedblock; -  -- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL -Placeholderimage: (Nullable UIImage *) Placeholder + options: (sdwebimageoptions) Options - Completed: (nullable sdexternalcompletionblock) Completedblock; +  A- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL atPlaceholderimage: (Nullable UIImage *) Placeholder - options: (sdwebimageoptions) Options - Progress: (Nullable Sdwebimagedownloaderprogressblock) Progressblock - Completed: (nullable sdexternalcompletionblock) Completedblock; -  -- (void) Sd_setimagewithpreviouscachedimagewithurl: (Nullable Nsurl *) URL inPlaceholderimage: (Nullable UIImage *) Placeholder - options: (sdwebimageoptions) Options to Progress: (Nullable Sdwebimagedownloaderprogressblock) Progressblock +Completed: (nullable sdexternalcompletionblock) Completedblock;

Jumping to the specific implementations of these methods, it is obvious that they eventually call this method of Uiview+webcache (this method is then explained):

-(void) Sd_internalsetimagewithurl: (Nullable Nsurl *) URL                  *) Placeholder                           options: ( sdwebimageoptions) Options                      *) Operationkey                     setimageblock: (Nullable Sdsetimageblock) Setimageblock                          Progress: (Nullable Sdwebimagedownloaderprogressblock) Progressblock                         completed: (Nullable Sdexternalcompletionblock) Completedblock;

There's a little bit of attention below:

Underlined three-mesh operator, which returns the condition itself by default if the first argument after the question mark is not filled in.

So next, there are two ways to interpret this class:

1 /* * 2 * Download An array of images and starts them in an animation loop 3  * 4 * @param arrayofurls An array of Nsurl 5  */ 6 -(void) Sd_setanimationimageswithurls: (nonnull nsarray<nsurl *> *) arrayofurls; 7 8 -(void) sd_cancelcurrentanimationimagesload;

According to the method declaration, we learned that this is an array of frame animation images used to load Uiimageview. We look directly at the concrete implementation of the method:

- (void) Sd_setanimationimageswithurls: (nonnull nsarray<nsurl *> *) Arrayofurls {[Self sd_cancelcurrentanimationimagesload]; __weak __typeof (self) wself=Self ; Nsmutablearray<ID<SDWebImageOperation>> *operationsarray =[[Nsmutablearray alloc] init];  for(Nsurl *logoimageurlincharrayofurls) {        ID<SDWebImageOperation> operation = [Sdwebimagemanager.sharedmanager loadimagewithurl:logoimageurl options:0Progress:nil completed:^ (UIImage *image, NSData *data, Nserror *error, Sdimagecachetype CacheType, BOOL finished, NSURL *ImageURL) {            if(!wself)return; Dispatch_main_async_safe (^{__strong Uiimageview*sself =wself;                [Sself stopanimating]; if(Sself &&image) {Nsmutablearray<uiimage *> *currentimages =[[Sself animationimages] mutablecopy]; if(!currentimages) {Currentimages=[[Nsmutablearray alloc] init];                    } [Currentimages Addobject:image]; Sself.animationimages=currentimages;                [Sself Setneedslayout];            } [Sself startanimating];        });        }];    [Operationsarray addobject:operation]; } [self Sd_setimageloadoperation:[operationsarray copy] Forkey:@"uiimageviewanimationimages"];}- (void) sd_cancelcurrentanimationimagesload {[Self Sd_cancelimageloadoperationwithkey:@"uiimageviewanimationimages"];}

As we can see, this method iterates through the image URL array and then uses the Sdwebimagemanager method to place each picture loaded into the operation array, and then calls:

[Self sd_setimageloadoperation:[operationsarray copy] forkey:@ "Uiimageviewanimationimages"];

This method is uiview+webcacheoperation, which stores all operations in a dictionary.

Inside this method, we see "Dispatch_main_async_safe"

1 #ifndef Dispatch_main_async_safe2 #defineDispatch_main_async_safe (Block)3     if(strcmp (Dispatch_queue_get_label (Dispatch_current_queue_label), Dispatch_queue_get_label (dispatch_get_main_ Queue ())) = =0) {4 block ();5}Else {6 Dispatch_async (Dispatch_get_main_queue (), block);7     }8 #endif

This is a macro definition in the Sdwebimagecompat class. The goal is to ensure thread safety and to ensure that the code in the block executes in the main thread. When the picture is downloaded, it is updated uiimageview on the main thread.

-(void) sd_cancelcurrentanimationimagesload {    [self sd_cancelimageloadoperationwithkey:  @ "uiimageviewanimationimages"];}

The last method is to cancel the animationimages operation that is currently being loaded

Sdwebimage Source code Interpretation (iv) Uiimageview+webcache

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.