美文网首页
AFN加载图片,不读取本地缓存的问题

AFN加载图片,不读取本地缓存的问题

作者: 春暖花已开 | 来源:发表于2018-11-30 11:09 被阅读52次
    - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
                  placeholderImage:(UIImage *)placeholderImage
                           success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
                           failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
    {
        
        if ([urlRequest URL] == nil) {
            self.image = placeholderImage;
            if (failure) {
                NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
                failure(urlRequest, nil, error);
            }
            return;
        }
        
        if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){
            return;
        }
        
        [self cancelImageDownloadTask];
    
        AFImageDownloader *downloader = [[self class] sharedImageDownloader];
        id <AFImageRequestCache> imageCache = downloader.imageCache;
    
        //Use the image from the image cache if it exists
        UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
        if (cachedImage) {
            if (success) {
                success(urlRequest, nil, cachedImage);
            } else {
                self.image = cachedImage;
            }
            [self clearActiveDownloadInformation];
        } else {
            
            // 内存缓存没有,读取硬盘缓存
            NSURLCache *urlCache = downloader.sessionManager.session.configuration.URLCache;
            NSCachedURLResponse *cacheResponse = [urlCache cachedResponseForRequest:urlRequest];
            if (cacheResponse.data) {
                cachedImage = [UIImage imageWithData:cacheResponse.data];
                self.image = cachedImage;
                return;
            }
            
            if (placeholderImage) {
                self.image = placeholderImage;
            }
    
            __weak __typeof(self)weakSelf = self;
            NSUUID *downloadID = [NSUUID UUID];
            AFImageDownloadReceipt *receipt;
            receipt = [downloader
                       downloadImageForURLRequest:urlRequest
                       withReceiptID:downloadID
                       success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
                           __strong __typeof(weakSelf)strongSelf = weakSelf;
                           if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
                               if (success) {
                                   success(request, response, responseObject);
                               } else if(responseObject) {
                                   strongSelf.image = responseObject;
                               }
                               [strongSelf clearActiveDownloadInformation];
                           }
    
                       }
                       failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                           __strong __typeof(weakSelf)strongSelf = weakSelf;
                            if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
                                if (failure) {
                                    failure(request, response, error);
                                }
                                [strongSelf clearActiveDownloadInformation];
                            }
                       }];
    
            self.af_activeImageDownloadReceipt = receipt;
        }
    }
    

    参考

    相关文章

      网友评论

          本文标题:AFN加载图片,不读取本地缓存的问题

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