/* * This file is part of the SDWebImage package. * (c) Olivier Poitrey * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ #import "EMSDWebImageManager.h" #import @interface EMSDWebImageCombinedOperation : NSObject @property (assign, nonatomic, getter = isCancelled) BOOL cancelled; @property (copy, nonatomic) EMSDWebImageNoParamsBlock cancelBlock; @property (strong, nonatomic) NSOperation *cacheOperation; @end @interface EMSDWebImageManager () @property (strong, nonatomic, readwrite) EMSDImageCache *imageCache; @property (strong, nonatomic, readwrite) EMSDWebImageDownloader *imageDownloader; @property (strong, nonatomic) NSMutableArray *failedURLs; @property (strong, nonatomic) NSMutableArray *runningOperations; @end @implementation EMSDWebImageManager + (id)sharedManager { static dispatch_once_t once; static id instance; dispatch_once(&once, ^{ instance = [self new]; }); return instance; } - (id)init { if ((self = [super init])) { _imageCache = [self createCache]; _imageDownloader = [EMSDWebImageDownloader sharedDownloader]; _failedURLs = [NSMutableArray new]; _runningOperations = [NSMutableArray new]; } return self; } - (EMSDImageCache *)createCache { return [EMSDImageCache sharedImageCache]; } - (NSString *)cacheKeyForURL:(NSURL *)url { if (self.cacheKeyFilter) { return self.cacheKeyFilter(url); } else { return [url absoluteString]; } } - (BOOL)cachedImageExistsForURL:(NSURL *)url { NSString *key = [self cacheKeyForURL:url]; if ([self.imageCache imageFromMemoryCacheForKey:key] != nil) return YES; return [self.imageCache diskImageExistsWithKey:key]; } - (BOOL)diskImageExistsForURL:(NSURL *)url { NSString *key = [self cacheKeyForURL:url]; return [self.imageCache diskImageExistsWithKey:key]; } - (void)cachedImageExistsForURL:(NSURL *)url completion:(EMSDWebImageCheckCacheCompletionBlock)completionBlock { NSString *key = [self cacheKeyForURL:url]; BOOL isInMemoryCache = ([self.imageCache imageFromMemoryCacheForKey:key] != nil); if (isInMemoryCache) { // making sure we call the completion block on the main queue dispatch_async(dispatch_get_main_queue(), ^{ if (completionBlock) { completionBlock(YES); } }); return; } [self.imageCache diskImageExistsWithKey:key completion:^(BOOL isInDiskCache) { // the completion block of checkDiskCacheForImageWithKey:completion: is always called on the main queue, no need to further dispatch if (completionBlock) { completionBlock(isInDiskCache); } }]; } - (void)diskImageExistsForURL:(NSURL *)url completion:(EMSDWebImageCheckCacheCompletionBlock)completionBlock { NSString *key = [self cacheKeyForURL:url]; [self.imageCache diskImageExistsWithKey:key completion:^(BOOL isInDiskCache) { // the completion block of checkDiskCacheForImageWithKey:completion: is always called on the main queue, no need to further dispatch if (completionBlock) { completionBlock(isInDiskCache); } }]; } - (id )downloadImageWithURL:(NSURL *)url options:(EMSDWebImageOptions)options progress:(EMSDWebImageDownloaderProgressBlock)progressBlock completed:(EMSDWebImageCompletionWithFinishedBlock)completedBlock { // Invoking this method without a completedBlock is pointless NSParameterAssert(completedBlock); // Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't // throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString. if ([url isKindOfClass:NSString.class]) { url = [NSURL URLWithString:(NSString *)url]; } // Prevents app crashing on argument type error like sending NSNull instead of NSURL if (![url isKindOfClass:NSURL.class]) { url = nil; } __block EMSDWebImageCombinedOperation *operation = [EMSDWebImageCombinedOperation new]; __weak EMSDWebImageCombinedOperation *weakOperation = operation; BOOL isFailedUrl = NO; @synchronized (self.failedURLs) { isFailedUrl = [self.failedURLs containsObject:url]; } if (!url || (!(options & EMSDWebImageRetryFailed) && isFailedUrl)) { dispatch_main_sync_safe(^{ NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]; completedBlock(nil, error, EMSDImageCacheTypeNone, YES, url); }); return operation; } @synchronized (self.runningOperations) { [self.runningOperations addObject:operation]; } NSString *key = [self cacheKeyForURL:url]; operation.cacheOperation = [self.imageCache queryDiskCacheForKey:key done:^(UIImage *image, EMSDImageCacheType cacheType) { if (operation.isCancelled) { @synchronized (self.runningOperations) { [self.runningOperations removeObject:operation]; } return; } if ((!image || options & EMSDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) { if (image && options & EMSDWebImageRefreshCached) { dispatch_main_sync_safe(^{ // If image was found in the cache bug EMSDWebImageRefreshCached is provided, notify about the cached image // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server. completedBlock(image, nil, cacheType, YES, url); }); } // download if no image or requested to refresh anyway, and download allowed by delegate EMSDWebImageDownloaderOptions downloaderOptions = 0; if (options & EMSDWebImageLowPriority) downloaderOptions |= EMSDWebImageDownloaderLowPriority; if (options & EMSDWebImageProgressiveDownload) downloaderOptions |= EMSDWebImageDownloaderProgressiveDownload; if (options & EMSDWebImageRefreshCached) downloaderOptions |= EMSDWebImageDownloaderUseNSURLCache; if (options & EMSDWebImageContinueInBackground) downloaderOptions |= EMSDWebImageDownloaderContinueInBackground; if (options & EMSDWebImageHandleCookies) downloaderOptions |= EMSDWebImageDownloaderHandleCookies; if (options & EMSDWebImageAllowInvalidSSLCertificates) downloaderOptions |= EMSDWebImageDownloaderAllowInvalidSSLCertificates; if (options & EMSDWebImageHighPriority) downloaderOptions |= EMSDWebImageDownloaderHighPriority; if (image && options & EMSDWebImageRefreshCached) { // force progressive off if image already cached but forced refreshing downloaderOptions &= ~EMSDWebImageDownloaderProgressiveDownload; // ignore image read from NSURLCache if image if cached but force refreshing downloaderOptions |= EMSDWebImageDownloaderIgnoreCachedResponse; } id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { if (weakOperation.isCancelled) { // Do nothing if the operation was cancelled // See #699 for more details // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data } else if (error) { dispatch_main_sync_safe(^{ if (!weakOperation.isCancelled) { completedBlock(nil, error, EMSDImageCacheTypeNone, finished, url); } }); if (error.code != NSURLErrorNotConnectedToInternet && error.code != NSURLErrorCancelled && error.code != NSURLErrorTimedOut) { @synchronized (self.failedURLs) { [self.failedURLs addObject:url]; } } } else { BOOL cacheOnDisk = !(options & EMSDWebImageCacheMemoryOnly); if (options & EMSDWebImageRefreshCached && image && !downloadedImage) { // Image refresh hit the NSURLCache cache, do not call the completion block } // NOTE: We don't call transformDownloadedImage delegate method on animated images as most transformation code would mangle it else if (downloadedImage && !downloadedImage.images && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)]) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; if (transformedImage && finished) { BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; [self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:data forKey:key toDisk:cacheOnDisk]; } dispatch_main_sync_safe(^{ if (!weakOperation.isCancelled) { completedBlock(transformedImage, nil, EMSDImageCacheTypeNone, finished, url); } }); }); } else { if (downloadedImage && finished) { [self.imageCache storeImage:downloadedImage recalculateFromImage:NO imageData:data forKey:key toDisk:cacheOnDisk]; } dispatch_main_sync_safe(^{ if (!weakOperation.isCancelled) { completedBlock(downloadedImage, nil, EMSDImageCacheTypeNone, finished, url); } }); } } if (finished) { @synchronized (self.runningOperations) { [self.runningOperations removeObject:operation]; } } }]; operation.cancelBlock = ^{ [subOperation cancel]; @synchronized (self.runningOperations) { [self.runningOperations removeObject:weakOperation]; } }; } else if (image) { dispatch_main_sync_safe(^{ if (!weakOperation.isCancelled) { completedBlock(image, nil, cacheType, YES, url); } }); @synchronized (self.runningOperations) { [self.runningOperations removeObject:operation]; } } else { // Image not in cache and download disallowed by delegate dispatch_main_sync_safe(^{ if (!weakOperation.isCancelled) { completedBlock(nil, nil, EMSDImageCacheTypeNone, YES, url); } }); @synchronized (self.runningOperations) { [self.runningOperations removeObject:operation]; } } }]; return operation; } - (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url { if (image && url) { NSString *key = [self cacheKeyForURL:url]; [self.imageCache storeImage:image forKey:key toDisk:YES]; } } - (void)cancelAll { @synchronized (self.runningOperations) { [self.runningOperations makeObjectsPerformSelector:@selector(cancel)]; [self.runningOperations removeAllObjects]; } } - (BOOL)isRunning { return self.runningOperations.count > 0; } @end @implementation EMSDWebImageCombinedOperation - (void)setCancelBlock:(EMSDWebImageNoParamsBlock)cancelBlock { // check if the operation is already cancelled, then we just call the cancelBlock if (self.isCancelled) { if (cancelBlock) { cancelBlock(); } _cancelBlock = nil; // don't forget to nil the cancelBlock, otherwise we will get crashes } else { _cancelBlock = [cancelBlock copy]; } } - (void)cancel { self.cancelled = YES; if (self.cacheOperation) { [self.cacheOperation cancel]; self.cacheOperation = nil; } if (self.cancelBlock) { self.cancelBlock(); // TODO: this is a temporary fix to #809. // Until we can figure the exact cause of the crash, going with the ivar instead of the setter // self.cancelBlock = nil; _cancelBlock = nil; } } @end @implementation EMSDWebImageManager (Deprecated) // deprecated method, uses the non deprecated method // adapter for the completion block - (id )downloadWithURL:(NSURL *)url options:(EMSDWebImageOptions)options progress:(EMSDWebImageDownloaderProgressBlock)progressBlock completed:(EMSDWebImageCompletedWithFinishedBlock)completedBlock { return [self downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, EMSDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (completedBlock) { completedBlock(image, error, cacheType, finished); } }]; } @end