// // YBIBDefaultWebImageMediator.m // YBImageBrowserDemo // // Created by 波儿菜 on 2019/8/27. // Copyright © 2019 杨波. All rights reserved. // #import "YBIBDefaultWebImageMediator.h" #import "YBIBUtilities.h" #if __has_include() #import #else #import "SDWebImageManager.h" #endif @implementation YBIBDefaultWebImageMediator #pragma mark - - (id)yb_downloadImageWithURL:(NSURL *)URL requestModifier:(nullable YBIBWebImageRequestModifierBlock)requestModifier progress:(nonnull YBIBWebImageProgressBlock)progress success:(nonnull YBIBWebImageSuccessBlock)success failed:(nonnull YBIBWebImageFailedBlock)failed { if (!URL) return nil; SDWebImageDownloaderOptions options = SDWebImageDownloaderUseNSURLCache; SDWebImageDownloadToken *token = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:URL options:options progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { if (progress) progress(receivedSize, expectedSize); } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { if (error) { if (failed) failed(error, finished); } else { if (success) success(data, finished); } }]; return token; } - (void)yb_cancelTaskWithDownloadToken:(id)token { if (token && [token isKindOfClass:SDWebImageDownloadToken.class]) { [((SDWebImageDownloadToken *)token) cancel]; } } - (void)yb_storeToDiskWithImageData:(NSData *)data forKey:(NSURL *)key { if (!key) return; NSString *cacheKey = [SDWebImageManager.sharedManager cacheKeyForURL:key]; if (!cacheKey) return; YBIB_DISPATCH_ASYNC(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:cacheKey]; }) } - (void)yb_queryCacheOperationForKey:(NSURL *)key completed:(YBIBWebImageCacheQueryCompletedBlock)completed { #define QUERY_CACHE_FAILED if (completed) {completed(nil, nil); return;} if (!key) QUERY_CACHE_FAILED NSString *cacheKey = [SDWebImageManager.sharedManager cacheKeyForURL:key]; if (!cacheKey) QUERY_CACHE_FAILED #undef QUERY_CACHE_FAILED // 'NSData' of image must be read to ensure decoding correctly. SDImageCacheOptions options = SDImageCacheQueryDataWhenInMemory | SDImageCacheScaleDownLargeImages; [[SDImageCache sharedImageCache] queryCacheOperationForKey:cacheKey options:options done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) { if (completed) completed(image, data); }]; } @end