美文网首页YYKit学习YYKit源码探究
YYKit源码探究(三十五) —— UIButton分类之ima

YYKit源码探究(三十五) —— UIButton分类之ima

作者: 刀客传奇 | 来源:发表于2018-03-29 20:15 被阅读50次

    版本记录

    版本号 时间
    V1.0 2018.03.29

    前言

    iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
    1. YYKit源码探究(一) —— 基本概览
    2. YYKit源码探究(二) —— NSString分类之Hash(一)
    3. YYKit源码探究(三) —— NSString分类之Encode and decode(二)
    4. YYKit源码探究(四) —— NSString分类之Drawing(三)
    5. YYKit源码探究(五) —— NSString分类之Regular Expression(四)
    6. YYKit源码探究(六) —— NSString分类之NSNumber Compatible(五)
    7. YYKit源码探究(七) —— NSString分类之Utilities(六)
    8. YYKit源码探究(八) —— NSNumber分类(一)
    9. YYKit源码探究(九) —— UIFont分类之架构分析和Font Traits(一)
    10. YYKit源码探究(十) —— UIFont分类之Create font(二)
    11. YYKit源码探究(十一) —— UIFont分类之Load and unload font(三)
    12. YYKit源码探究(十二) —— UIFont分类之Dump font data(四)
    13. YYKit源码探究(十三) —— UIImage分类之框架结构和Create image部分(一)
    14. YYKit源码探究(十四) —— UIImage分类之Image Info(二)
    15. YYKit源码探究(十五) —— UIImage分类之Modify Image(三)
    16. YYKit源码探究(十六) —— UIImage分类之Image Effect(四)
    17. YYKit源码探究(十七) —— UIImageView分类之架构和image部分(一)
    18. YYKit源码探究(十八) —— UIImageView分类之highlight image部分(二)
    19. YYKit源码探究(十九) —— UIScreen分类(一)
    20. YYKit源码探究(二十) —— UIScrollView分类(一)
    21. YYKit源码探究(二十一) —— UITableView分类(一)
    22. YYKit源码探究(二十二) —— UITextField分类(一)
    23. YYKit源码探究(二十三) —— UIView分类(一)
    24. YYKit源码探究(二十四) —— UIPasteboard分类(一)
    25. YYKit源码探究(二十五) —— UIGestureRecognizer分类(一)
    26. YYKit源码探究(二十六) —— UIDevice分类框架及Device Information(一)
    27. YYKit源码探究(二十七) —— UIDevice分类之Network Information(二)
    28. YYKit源码探究(二十八) —— UIDevice分类之Disk Space(三)
    29. YYKit源码探究(二十九) —— UIDevice分类之Memory Information(四)
    30. YYKit源码探究(三十) —— UIDevice分类之CPU Information(五)
    31. YYKit源码探究(三十一) —— UIControl分类(一)
    32. YYKit源码探究(三十二) —— UIColor分类之Create a UIColor Object(一)
    33. YYKit源码探究(三十三) —— UIColor分类之Get color's description(二)
    34. YYKit源码探究(三十四) —— UIColor分类之Retrieving Color Information(三)

    回顾

    上一篇主要介绍了UIColor的Retrieving Color Information分类,这一篇主要看一下UIButton分类image部分。


    框架结构

    下面我们就一起看一下框架结构。


    API

    下面我们就一起看一下API - image文档部分。

    /**
     Current image URL for the specified state.
     @return The image URL, or nil.
     */
    - (nullable NSURL *)imageURLForState:(UIControlState)state;
    
    /**
     Set the button's image with a specified URL for the specified state.
     
     @param imageURL    The image url (remote or local file path).
     @param state       The state that uses the specified image.
     @param placeholder The image to be set initially, until the image request finishes.
     */
    - (void)setImageWithURL:(nullable NSURL *)imageURL
                      forState:(UIControlState)state
                   placeholder:(nullable UIImage *)placeholder;
    
    /**
     Set the button's image with a specified URL for the specified state.
     
     @param imageURL The image url (remote or local file path).
     @param state    The state that uses the specified image.
     @param options  The options to use when request the image.
     */
    - (void)setImageWithURL:(nullable NSURL *)imageURL
                      forState:(UIControlState)state
                       options:(YYWebImageOptions)options;
    
    /**
     Set the button's image with a specified URL for the specified state.
     
     @param imageURL    The image url (remote or local file path).
     @param state       The state that uses the specified image.
     @param placeholder The image to be set initially, until the image request finishes.
     @param options     The options to use when request the image.
     @param completion  The block invoked (on main thread) when image request completed.
     */
    - (void)setImageWithURL:(nullable NSURL *)imageURL
                      forState:(UIControlState)state
                   placeholder:(nullable UIImage *)placeholder
                       options:(YYWebImageOptions)options
                    completion:(nullable YYWebImageCompletionBlock)completion;
    
    /**
     Set the button's image with a specified URL for the specified state.
     
     @param imageURL    The image url (remote or local file path).
     @param state       The state that uses the specified image.
     @param placeholder The image to be set initially, until the image request finishes.
     @param options     The options to use when request the image.
     @param progress    The block invoked (on main thread) during image request.
     @param transform   The block invoked (on background thread) to do additional image process.
     @param completion  The block invoked (on main thread) when image request completed.
     */
    - (void)setImageWithURL:(nullable NSURL *)imageURL
                      forState:(UIControlState)state
                   placeholder:(nullable UIImage *)placeholder
                       options:(YYWebImageOptions)options
                      progress:(nullable YYWebImageProgressBlock)progress
                     transform:(nullable YYWebImageTransformBlock)transform
                    completion:(nullable YYWebImageCompletionBlock)completion;
    
    /**
     Set the button's image with a specified URL for the specified state.
     
     @param imageURL    The image url (remote or local file path).
     @param state       The state that uses the specified image.
     @param placeholder The image to be set initially, until the image request finishes.
     @param options     The options to use when request the image.
     @param manager     The manager to create image request operation.
     @param progress    The block invoked (on main thread) during image request.
     @param transform   The block invoked (on background thread) to do additional image process.
     @param completion  The block invoked (on main thread) when image request completed.
     */
    - (void)setImageWithURL:(nullable NSURL *)imageURL
                      forState:(UIControlState)state
                   placeholder:(nullable UIImage *)placeholder
                       options:(YYWebImageOptions)options
                       manager:(nullable YYWebImageManager *)manager
                      progress:(nullable YYWebImageProgressBlock)progress
                     transform:(nullable YYWebImageTransformBlock)transform
                    completion:(nullable YYWebImageCompletionBlock)completion;
    
    /**
     Cancel the current image request for a specified state.
     @param state The state that uses the specified image.
     */
    - (void)cancelImageRequestForState:(UIControlState)state;
    

    下面我们就详细的看一下这个API文档。

    1. - (nullable NSURL *)imageURLForState:(UIControlState)state;

    返回按钮指定状态的URL地址。

    方法实现

    - (NSURL *)imageURLForState:(UIControlState)state {
        _YYWebImageSetterDicForButton *dic = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
        _YYWebImageSetter *setter = [dic setterForState:UIControlStateSingle(state)];
        return setter.imageURL;
    }
    

    2. - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder;

    给按钮设置指定URL地址的图像。

    方法实现

    - (void)setImageWithURL:(NSURL *)imageURL forState:(UIControlState)state placeholder:(UIImage *)placeholder {
        [self setImageWithURL:imageURL
                     forState:state
                  placeholder:placeholder
                      options:kNilOptions
                      manager:nil
                     progress:nil
                    transform:nil
                   completion:nil];
    }
    
    - (void)setImageWithURL:(NSURL *)imageURL
                   forState:(UIControlState)state
                placeholder:(UIImage *)placeholder
                    options:(YYWebImageOptions)options
                    manager:(YYWebImageManager *)manager
                   progress:(YYWebImageProgressBlock)progress
                  transform:(YYWebImageTransformBlock)transform
                 completion:(YYWebImageCompletionBlock)completion {
        for (NSNumber *num in UIControlStateMulti(state)) {
            [self _setImageWithURL:imageURL
                    forSingleState:num
                       placeholder:placeholder
                           options:options
                           manager:manager
                          progress:progress
                         transform:transform
                        completion:completion];
        }
    }
    
    - (void)_setImageWithURL:(NSURL *)imageURL
              forSingleState:(NSNumber *)state
                 placeholder:(UIImage *)placeholder
                     options:(YYWebImageOptions)options
                     manager:(YYWebImageManager *)manager
                    progress:(YYWebImageProgressBlock)progress
                   transform:(YYWebImageTransformBlock)transform
                  completion:(YYWebImageCompletionBlock)completion {
        if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL];
        manager = manager ? manager : [YYWebImageManager sharedManager];
        
        _YYWebImageSetterDicForButton *dic = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
        if (!dic) {
            dic = [_YYWebImageSetterDicForButton new];
            objc_setAssociatedObject(self, &_YYWebImageSetterKey, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
        _YYWebImageSetter *setter = [dic lazySetterForState:state];
        int32_t sentinel = [setter cancelWithNewURL:imageURL];
        
        dispatch_async_on_main_queue(^{
            if (!imageURL) {
                if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
                    [self setImage:placeholder forState:state.integerValue];
                }
                return;
            }
            
            // get the image from memory as quickly as possible
            UIImage *imageFromMemory = nil;
            if (manager.cache &&
                !(options & YYWebImageOptionUseNSURLCache) &&
                !(options & YYWebImageOptionRefreshImageCache)) {
                imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory];
            }
            if (imageFromMemory) {
                if (!(options & YYWebImageOptionAvoidSetImage)) {
                    [self setImage:imageFromMemory forState:state.integerValue];
                }
                if(completion) completion(imageFromMemory, imageURL, YYWebImageFromMemoryCacheFast, YYWebImageStageFinished, nil);
                return;
            }
            
            
            if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
                [self setImage:placeholder forState:state.integerValue];
            }
            
            __weak typeof(self) _self = self;
            dispatch_async([_YYWebImageSetter setterQueue], ^{
                YYWebImageProgressBlock _progress = nil;
                if (progress) _progress = ^(NSInteger receivedSize, NSInteger expectedSize) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        progress(receivedSize, expectedSize);
                    });
                };
                
                __block int32_t newSentinel = 0;
                __block __weak typeof(setter) weakSetter = nil;
                YYWebImageCompletionBlock _completion = ^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
                    __strong typeof(_self) self = _self;
                    BOOL setImage = (stage == YYWebImageStageFinished || stage == YYWebImageStageProgress) && image && !(options & YYWebImageOptionAvoidSetImage);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        BOOL sentinelChanged = weakSetter && weakSetter.sentinel != newSentinel;
                        if (setImage && self && !sentinelChanged) {
                            [self setImage:image forState:state.integerValue];
                        }
                        if (completion) {
                            if (sentinelChanged) {
                                completion(nil, url, YYWebImageFromNone, YYWebImageStageCancelled, nil);
                            } else {
                                completion(image, url, from, stage, error);
                            }
                        }
                    });
                };
                
                newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion];
                weakSetter = setter;
            });
        });
    }
    

    3. - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state options:(YYWebImageOptions)options;

    给按钮设置指定URL地址的图像。

    方法实现

    - (void)setImageWithURL:(NSURL *)imageURL forState:(UIControlState)state options:(YYWebImageOptions)options {
        [self setImageWithURL:imageURL
                     forState:state
                  placeholder:nil
                      options:options
                      manager:nil
                     progress:nil
                    transform:nil
                   completion:nil];
    }
    

    4. - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options completion:(nullable YYWebImageCompletionBlock)completion;

    给按钮设置指定URL地址的图像。

    方法实现

    - (void)setImageWithURL:(NSURL *)imageURL
                   forState:(UIControlState)state
                placeholder:(UIImage *)placeholder
                    options:(YYWebImageOptions)options
                 completion:(YYWebImageCompletionBlock)completion {
        [self setImageWithURL:imageURL
                     forState:state
                  placeholder:placeholder
                      options:options
                      manager:nil
                     progress:nil
                    transform:nil
                   completion:completion];
    }
    

    5. - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion;

    给按钮设置指定URL地址的图像。

    方法实现

    - (void)setImageWithURL:(NSURL *)imageURL
                   forState:(UIControlState)state
                placeholder:(UIImage *)placeholder
                    options:(YYWebImageOptions)options
                   progress:(YYWebImageProgressBlock)progress
                  transform:(YYWebImageTransformBlock)transform
                 completion:(YYWebImageCompletionBlock)completion {
        [self setImageWithURL:imageURL
                     forState:state
                  placeholder:placeholder
                      options:options
                      manager:nil
                     progress:progress
                    transform:transform
                   completion:completion];
    }
    

    6. - (void)setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion;

    给按钮设置指定URL地址的图像。

    方法实现

    - (void)setImageWithURL:(NSURL *)imageURL
                   forState:(UIControlState)state
                placeholder:(UIImage *)placeholder
                    options:(YYWebImageOptions)options
                    manager:(YYWebImageManager *)manager
                   progress:(YYWebImageProgressBlock)progress
                  transform:(YYWebImageTransformBlock)transform
                 completion:(YYWebImageCompletionBlock)completion {
        for (NSNumber *num in UIControlStateMulti(state)) {
            [self _setImageWithURL:imageURL
                    forSingleState:num
                       placeholder:placeholder
                           options:options
                           manager:manager
                          progress:progress
                         transform:transform
                        completion:completion];
        }
    }
    

    7. - (void)cancelImageRequestForState:(UIControlState)state;

    取消当前图像对指定状态的请求。

    方法实现

    - (void)cancelImageRequestForState:(UIControlState)state {
        for (NSNumber *num in UIControlStateMulti(state)) {
            [self _cancelImageRequestForSingleState:num];
        }
    }
    
    - (void)_cancelImageRequestForSingleState:(NSNumber *)state {
        _YYWebImageSetterDicForButton *dic = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
        _YYWebImageSetter *setter = [dic setterForState:state];
        if (setter) [setter cancel];
    }
    
    - (_YYWebImageSetter *)setterForState:(NSNumber *)state {
        dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
        _YYWebImageSetter *setter = _dic[state];
        dispatch_semaphore_signal(_lock);
        return setter;    
    }
    

    后记

    本篇主要讲述了UIButton的image部分,感兴趣的可以给个赞和关注~~~~

    相关文章

      网友评论

      本文标题:YYKit源码探究(三十五) —— UIButton分类之ima

      本文链接:https://www.haomeiwen.com/subject/ztdvcftx.html