//SDWebImageDownloader.m-(nullableSDWebImageDownloadToken*)downloadImageWithURL:(nullableNSURL*)urloptions:(SDWebImageDownloaderOptions)optionsprogress:(nullableSDWebImageDownloaderProgressBlock)progressBlockcompleted:(nullableSDWebImageDownloaderCompletedBlock)completedBlock{LOCK(self.operationsLock);NSOperation<SDWebImageDownloaderOperationInterface>*operation=[self.URLOperationsobjectForKey:url];// There is a case that the operation may be marked as finished or cancelled, but not been removed from `self.URLOperations`.if(!operation||operation.isFinished||operation.isCancelled){operation=[selfcreateDownloaderOperationWithUrl:urloptions:options];__weaktypeof(self)wself=self;operation.completionBlock=^{__strongtypeof(wself)sself=wself;if(!sself){return;}LOCK(sself.operationsLock);[sself.URLOperationsremoveObjectForKey:url];UNLOCK(sself.operationsLock);};[self.URLOperationssetObject:operationforKey:url];// Add operation to operation queue only after all configuration done according to Apple's doc.// `addOperation:` does not synchronously execute the `operation.completionBlock` so this will not cause deadlock.//执行下载操作[self.downloadQueueaddOperation:operation];}UNLOCK(self.operationsLock);iddownloadOperationCancelToken=[operationaddHandlersForProgress:progressBlockcompleted:completedBlock];SDWebImageDownloadToken*token=[SDWebImageDownloadTokennew];token.downloadOperation=operation;token.url=url;token.downloadOperationCancelToken=downloadOperationCancelToken;returntoken;}
UIImage*transformedImage=[self.delegateimageManager:selftransformDownloadedImage:downloadedImagewithURL:url];if(transformedImage&&finished){BOOLimageWasTransformed=![transformedImageisEqual:downloadedImage];NSData*cacheData;// pass nil if the image was transformed, so we can recalculate the data from the imageif(self.cacheSerializer){cacheData=self.cacheSerializer(transformedImage,(imageWasTransformed?nil:downloadedData),url);}else{cacheData=(imageWasTransformed?nil:downloadedData);}[self.imageCachestoreImage:transformedImageimageData:cacheDataforKey:keytoDisk:cacheOnDiskcompletion:nil];}[selfcallCompletionBlockForOperation:strongSubOperationcompletion:completedBlockimage:transformedImagedata:downloadedDataerror:nilcacheType:SDImageCacheTypeNonefinished:finishedurl:url];