Sdwebimage Source code Interpretation (iv) Uibutton+webcache

Source: Internet
Author: User

Uibutton+webcache.h file

1. Gets the picture URL of the current button.

-(Nullable Nsurl *) Sd_currentimageurl;

2. Obtain the image URL according to the different status.

-(Nullable Nsurl *) Sd_imageurlforstate: (uicontrolstate) state;

3. Set the URL of the button in a different state, then load asynchronously and cache.

1- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL2 forstate: (uicontrolstate) state;3 4- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL5 forstate: (uicontrolstate) state6Placeholderimage: (Nullable UIImage *) placeholder;7 8- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL9 forstate: (uicontrolstate) stateTenPlaceholderimage: (Nullable UIImage *) Placeholder One options: (sdwebimageoptions) options; A  -- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL - forstate: (uicontrolstate) state the Completed: (nullable sdexternalcompletionblock) Completedblock; -  -- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL - forstate: (uicontrolstate) state +Placeholderimage: (Nullable UIImage *) Placeholder -Completed: (nullable sdexternalcompletionblock) Completedblock;

The implementations of these methods all call this method implementation:

1 -(void) Sd_setimagewithurl: (Nullable Nsurl *) URL2                  forstate: ( uicontrolstate)State3           placeholderimage: (Nullable UIImage *) placeholder4                    options: (sdwebimageoptions) options5                  completed: (Nullable Sdexternalcompletionblock) Completedblock;

4. Let's look at these several ways:

1- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL2 forstate: (uicontrolstate) state;3 4- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL5 forstate: (uicontrolstate) state6Placeholderimage: (Nullable UIImage *) placeholder;7 8- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL9 forstate: (uicontrolstate) stateTenPlaceholderimage: (Nullable UIImage *) Placeholder One options: (sdwebimageoptions) options; A  -- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL - forstate: (uicontrolstate) state the Completed: (nullable sdexternalcompletionblock) Completedblock; -  -- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL - forstate: (uicontrolstate) state +Placeholderimage: (Nullable UIImage *) Placeholder -Completed: (nullable sdexternalcompletionblock) Completedblock;

Again, these five methods are all called this method implementation, the function is to load the button background image asynchronously, and cache:

-(void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL2                             forstate: ( uicontrolstate)State3                      placeholderimage: (Nullable UIImage *) placeholder4                               options: (sdwebimageoptions) options5                             completed: (Nullable Sdexternalcompletionblock) Completedblock;

5. Then let's bang the method in the header file to finish reading:

 1  /*  *  2   * Cancel the current image download  3  */ 4 -( Span style= "COLOR: #0000ff" >void  ) Sd_cancelimageloadforstate: (uicontrolstate) State;  5  6  /*   *  7   *  Cancel the current backgroundimage download  8  */ 9 -(void ) sd_ Cancelbackgroundimageloadforstate: (uicontrolstate) state; 

These two methods function to cancel the image and backgroundimage of the button that is currently being downloaded

. M (the head file has been read and we already know where the focus of this class is.) Below we interpret. m files)

1. First we look at two get methods:

1-(Nullable Nsurl *) Sd_currentimageurl {2Nsurl *url =self.imageurlstorage[@ (self.state)];3 4     if(!URL) {5URL =self.imageurlstorage[@ (UIControlStateNormal)];6     }7 8     returnURL;9 }Ten  One-(Nullable Nsurl *) Sd_imageurlforstate: (uicontrolstate) state { A     returnself.imageurlstorage[@ (state)]; -}
1-(Sdstateimageurldictionary *) Imageurlstorage {2Sdstateimageurldictionary *storage = Objc_getassociatedobject (self, &imageurlstoragekey);3     if(!storage) {4Storage =[Nsmutabledictionary dictionary];5Objc_setassociatedobject (Self, &Imageurlstoragekey, storage, objc_association_retain_nonatomic);6     }7 8     returnstorage;9}
typedef nsmutabledictionary<nsnumber *, Nsurl *> sdstateimageurldictionary;

① look at the first method to get the URL, first find the URL in the dictionary named Imageurlstorage, and if the URL is nil, the URL of the UIControlStateNormal state is returned by default. The second method functions the same as the first method.

② We look at the lazy loading method of the following dictionary, the runtime is used, the attribute cannot be defined in the classification, but it can be added using the runtime method.

③typedef, defines a dictionary of the specified key and value types. We will be in the development of the project later, it can also be used, more secure and forced lattice.

2. OK, here's the big meal:

1- (void) Sd_setimagewithurl: (Nullable Nsurl *) URL2 forstate: (uicontrolstate) state3Placeholderimage: (Nullable UIImage *) Placeholder4 options: (sdwebimageoptions) Options5 Completed: (nullable Sdexternalcompletionblock) Completedblock {6     if(!URL) {7 [Self.imageurlstorage removeobjectforkey:@ (state)];8         return;9     }Ten      Oneself.imageurlstorage[@ (state)] =URL; A      -__weaktypeof(self) weakself =Self ; - [Self Sd_internalsetimagewithurl:url the Placeholderimage:placeholder - options:options -Operationkey:[nsstring stringWithFormat:@"uibuttonimageoperation%@", @ (state)] -setimageblock:^ (UIImage *image, NSData *imageData) { + [weakself setimage:image forstate:state]; -                        } + Progress:nil A Completed:completedblock]; at}
1- (void) Sd_setbackgroundimagewithurl: (Nullable Nsurl *) URL2 forstate: (uicontrolstate) state3Placeholderimage: (Nullable UIImage *) Placeholder4 options: (sdwebimageoptions) Options5 Completed: (nullable Sdexternalcompletionblock) Completedblock {6     if(!URL) {7 [Self.imageurlstorage removeobjectforkey:@ (state)];8         return;9     }Ten      Oneself.imageurlstorage[@ (state)] =URL; A      -__weaktypeof(self) weakself =Self ; - [Self Sd_internalsetimagewithurl:url the Placeholderimage:placeholder - options:options -Operationkey:[nsstring stringWithFormat:@"uibuttonbackgroundimageoperation%@", @ (state)] -setimageblock:^ (UIImage *image, NSData *imageData) { + [weakself setbackgroundimage:image forstate:state]; -                        } + Progress:nil A Completed:completedblock]; at}

Can carefully see the implementation of these two methods, can't help but disappointed (the paper is ready, you show me this). Originally this goods also uses the UIView method.

3. Look at these several settings Imageload and cancel Imageload method

1- (void) Sd_setimageloadoperation: (ID<SDWebImageOperation>) Operation Forstate: (uicontrolstate) state {2[Self sd_setimageloadoperation:operation forkey:[nsstring stringWithFormat:@"uibuttonimageoperation%@", @ (state)];3 }4 5- (void) Sd_cancelimageloadforstate: (uicontrolstate) state {6[Self sd_cancelimageloadoperationwithkey:[nsstring stringWithFormat:@"uibuttonimageoperation%@", @ (state)];7 }8 9- (void) Sd_setbackgroundimageloadoperation: (ID<SDWebImageOperation>) Operation Forstate: (uicontrolstate) state {Ten[Self sd_setimageloadoperation:operation forkey:[nsstring stringWithFormat:@"uibuttonbackgroundimageoperation%@", @ (state)]; One } A  -- (void) Sd_cancelbackgroundimageloadforstate: (uicontrolstate) state { -[Self sd_cancelimageloadoperationwithkey:[nsstring stringWithFormat:@"uibuttonbackgroundimageoperation%@", @ (state)]; the}

Familiar, like the above methods, are called Uiview+webcacheoperation method to do store operation in the dictionary.

??, this class is also done, small summary of Uiimageview and UIButton these two categories, methods and functions similar, are the peripheral janitor's brother. The points worth learning are:

① uses runtime to add attributes to the taxonomy.

② uses typedef to customize a dictionary or array of the specified type.

??, little friends, let's meet Uiview+webcache.

Sdwebimage Source code Interpretation (iv) Uibutton+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.