SDWebImage加载图片添加淡入淡出动画

话不多说,直接改UIImageView+WebCache.m 的这个方法

- (void)sd_setImageWithURL:(nullable NSURL *)url

          placeholderImage:(nullable UIImage *)placeholder

                   options:(SDWebImageOptions)options

                  progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock

                 completed:(nullable SDExternalCompletionBlock)completedBlock {

//    [self sd_internalSetImageWithURL:url

//                    placeholderImage:placeholder

//                             options:options

//                        operationKey:nil

扫描二维码关注公众号,回复: 2090871 查看本文章

//                       setImageBlock:nil

//                            progress:progressBlock

//                           completed:completedBlock];

    ///图片加载的淡入淡出效果

    [self sd_cancelCurrentImageLoad];

    objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);


    if (!(options & SDWebImageDelayPlaceholder)) {

        dispatch_main_async_safe(^{

            self.image = placeholder;

        });

    }


    if (url) {


        // check if activityView is enabled or not

        if ([self sd_showActivityIndicatorView]) {

            [self sd_addActivityIndicator];

        }


        __weak __typeof(self)wself = self;

        id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

            [wself sd_removeActivityIndicator];

            if (!wself) return;

            dispatch_main_async_safe(^{

                if (!wself) return;

                if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock)

                {

                    completedBlock(image, error, cacheType, url);

                    return;

                }

                else if (image) {

                    CATransition *animation = [CATransition animation];

                    animation.duration = .85f;

                    animation.type = kCATransitionFade;

                    animation.removedOnCompletion = YES;

                    [wself.layer addAnimation:animation forKey:@"transition"];

                    wself.image = image;

                    [wself setNeedsLayout];

                } else {

                    if ((options & SDWebImageDelayPlaceholder)) {

                        wself.image = placeholder;

                        [wself setNeedsLayout];

                    }

                }

                if (completedBlock && finished) {

                    completedBlock(image, error, cacheType, url);

                }

            });

        }];

        [self.layer removeAnimationForKey:@"transition"];

        [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];

    } else {

        dispatch_main_async_safe(^{

            [self sd_removeActivityIndicator];

            if (completedBlock) {

                NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];

                completedBlock(nil, error, SDImageCacheTypeNone, url);

            }

        });

    }

}


猜你喜欢

转载自blog.csdn.net/yyjjyysleep/article/details/78130657