From 05889b6b9cd2ffd019dbe4ed912d2fecb58dc5be Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 21 Jul 2018 23:44:06 +0800 Subject: [PATCH 1/4] Replace valueForKey with objectForKey when access NSDictionary --- SDWebImage/SDImageAPNGCoder.m | 28 ++++++++++++---------- SDWebImage/SDImageCache.m | 4 ++-- SDWebImage/SDImageCacheDefine.m | 14 +++++------ SDWebImage/SDImageGIFCoder.m | 28 ++++++++++++---------- SDWebImage/SDImageIOCoder.m | 19 ++++++++------- SDWebImage/SDImageLoader.m | 28 ++++++++++------------ SDWebImage/SDWebImageDownloader.m | 12 ++++------ SDWebImage/SDWebImageDownloaderOperation.m | 11 ++++++--- SDWebImage/SDWebImageManager.m | 18 +++++++------- SDWebImage/UIView+WebCache.m | 12 ++++------ SDWebImage/WebP/SDImageWebPCoder.m | 26 +++++++++++--------- 11 files changed, 103 insertions(+), 97 deletions(-) diff --git a/SDWebImage/SDImageAPNGCoder.m b/SDWebImage/SDImageAPNGCoder.m index f6259775..a7cea27b 100644 --- a/SDWebImage/SDImageAPNGCoder.m +++ b/SDWebImage/SDImageAPNGCoder.m @@ -81,8 +81,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -139,9 +140,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef - (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source { NSUInteger loopCount = 0; NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil); - NSDictionary *pngProperties = [imageProperties valueForKey:(__bridge NSString *)kCGImagePropertyPNGDictionary]; + NSDictionary *pngProperties = imageProperties[(__bridge NSString *)kCGImagePropertyPNGDictionary]; if (pngProperties) { - NSNumber *apngLoopCount = [pngProperties valueForKey:(__bridge NSString *)kCGImagePropertyAPNGLoopCount]; + NSNumber *apngLoopCount = pngProperties[(__bridge NSString *)kCGImagePropertyAPNGLoopCount]; if (apngLoopCount != nil) { loopCount = apngLoopCount.unsignedIntegerValue; } @@ -199,8 +200,8 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef } NSMutableDictionary *properties = [NSMutableDictionary dictionary]; double compressionQuality = 1; - if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { - compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; + if (options[SDImageCoderEncodeCompressionQuality]) { + compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; @@ -246,8 +247,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatPNG]; _imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType}); CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -297,8 +299,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef if (partialImageRef) { CGFloat scale = _scale; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -334,8 +337,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 96ec13bc..2bcafc5e 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -355,9 +355,9 @@ return nil; } - if ([context valueForKey:SDWebImageContextImageTransformer]) { + id transformer = context[SDWebImageContextImageTransformer]; + if (transformer) { // grab the transformed disk image if transformer provided - id transformer = [context valueForKey:SDWebImageContextImageTransformer]; NSString *transformerKey = [transformer transformerKey]; key = SDTransformedKeyForKey(key, transformerKey); } diff --git a/SDWebImage/SDImageCacheDefine.m b/SDWebImage/SDImageCacheDefine.m index c6b7cc5a..e6ac6881 100644 --- a/SDWebImage/SDImageCacheDefine.m +++ b/SDWebImage/SDImageCacheDefine.m @@ -15,20 +15,18 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) { UIImage *image; BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; - NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; + NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); if (scale < 1) { scale = 1; } if (!decodeFirstFrame) { + Class animatedImageClass = context[SDWebImageContextAnimatedImageClass]; // check whether we should use `SDAnimatedImage` - if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { - Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; - if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { - image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; - if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) { - [((id)image) preloadAllFrames]; - } + if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { + image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; + if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) { + [((id)image) preloadAllFrames]; } } } diff --git a/SDWebImage/SDImageGIFCoder.m b/SDWebImage/SDImageGIFCoder.m index 8936cf7b..41cd4e00 100644 --- a/SDWebImage/SDImageGIFCoder.m +++ b/SDWebImage/SDImageGIFCoder.m @@ -74,8 +74,9 @@ return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -132,9 +133,9 @@ - (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source { NSUInteger loopCount = 1; NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil); - NSDictionary *gifProperties = [imageProperties valueForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]; + NSDictionary *gifProperties = imageProperties[(__bridge NSString *)kCGImagePropertyGIFDictionary]; if (gifProperties) { - NSNumber *gifLoopCount = [gifProperties valueForKey:(__bridge NSString *)kCGImagePropertyGIFLoopCount]; + NSNumber *gifLoopCount = gifProperties[(__bridge NSString *)kCGImagePropertyGIFLoopCount]; if (gifLoopCount != nil) { loopCount = gifLoopCount.unsignedIntegerValue; } @@ -186,8 +187,9 @@ CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF]; _imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType}); CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -237,8 +239,9 @@ if (partialImageRef) { CGFloat scale = _scale; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -282,8 +285,8 @@ } NSMutableDictionary *properties = [NSMutableDictionary dictionary]; double compressionQuality = 1; - if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { - compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; + if (options[SDImageCoderEncodeCompressionQuality]) { + compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; @@ -335,8 +338,9 @@ return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } diff --git a/SDWebImage/SDImageIOCoder.m b/SDWebImage/SDImageIOCoder.m index 11ef5cf0..7a34ee8e 100644 --- a/SDWebImage/SDImageIOCoder.m +++ b/SDWebImage/SDImageIOCoder.m @@ -65,8 +65,9 @@ return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -97,8 +98,9 @@ if (self) { _imageSource = CGImageSourceCreateIncremental(NULL); CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -153,8 +155,9 @@ if (partialImageRef) { CGFloat scale = _scale; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -220,8 +223,8 @@ #endif [properties setValue:@(exifOrientation) forKey:(__bridge NSString *)kCGImagePropertyOrientation]; double compressionQuality = 1; - if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { - compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; + if (options[SDImageCoderEncodeCompressionQuality]) { + compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; diff --git a/SDWebImage/SDImageLoader.m b/SDWebImage/SDImageLoader.m index dbcf7f52..4d9531ba 100644 --- a/SDWebImage/SDImageLoader.m +++ b/SDWebImage/SDImageLoader.m @@ -21,7 +21,7 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS NSCParameterAssert(imageURL); UIImage *image; - id cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; + id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; NSString *cacheKey; if (cacheKeyFilter) { cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL]; @@ -29,20 +29,18 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS cacheKey = imageURL.absoluteString; } BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; - NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; + NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); if (scale < 1) { scale = 1; } if (!decodeFirstFrame) { // check whether we should use `SDAnimatedImage` - if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { - Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; - if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { - image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; - if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) { - [((id)image) preloadAllFrames]; - } + Class animatedImageClass = context[SDWebImageContextAnimatedImageClass]; + if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { + image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; + if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) { + [((id)image) preloadAllFrames]; } } } @@ -78,7 +76,7 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im NSCParameterAssert(operation); UIImage *image; - id cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; + id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; NSString *cacheKey; if (cacheKeyFilter) { cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL]; @@ -86,7 +84,7 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im cacheKey = imageURL.absoluteString; } BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; - NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; + NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); if (scale < 1) { scale = 1; @@ -111,11 +109,9 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im [progressiveCoder updateIncrementalData:imageData finished:finished]; if (!decodeFirstFrame) { // check whether we should use `SDAnimatedImage` - if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { - Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; - if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)] && [progressiveCoder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) { - image = [[animatedImageClass alloc] initWithAnimatedCoder:(id)progressiveCoder scale:scale]; - } + Class animatedImageClass = context[SDWebImageContextAnimatedImageClass]; + if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)] && [progressiveCoder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) { + image = [[animatedImageClass alloc] initWithAnimatedCoder:(id)progressiveCoder scale:scale]; } } if (!image) { diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 7dd3c7aa..f5f638d8 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -173,10 +173,8 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; mutableRequest.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies); mutableRequest.HTTPShouldUsePipelining = YES; mutableRequest.allHTTPHeaderFields = sself.HTTPHeaders; - id requestModifier; - if ([context valueForKey:SDWebImageContextDownloadRequestModifier]) { - requestModifier = [context valueForKey:SDWebImageContextDownloadRequestModifier]; - } else { + id requestModifier = context[SDWebImageContextDownloadRequestModifier]; + if (!requestModifier) { requestModifier = self.requestModifier; } @@ -475,10 +473,8 @@ didReceiveResponse:(NSURLResponse *)response } - (id)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock { - UIImage *cachedImage; - if ([context valueForKey:SDWebImageContextLoaderCachedImage]) { - cachedImage = [context valueForKey:SDWebImageContextLoaderCachedImage]; - } + UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage]; + SDWebImageDownloaderOptions downloaderOptions = 0; if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority; if (options & SDWebImageProgressiveLoad) downloaderOptions |= SDWebImageDownloaderProgressiveLoad; diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 5232400b..e69426ce 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -103,10 +103,15 @@ typedef NSMutableDictionary SDCallbacksDictionary; - (nullable NSArray *)callbacksForKey:(NSString *)key { LOCK(self.callbacksLock); - NSMutableArray *callbacks = [[self.callbackBlocks valueForKey:key] mutableCopy]; + NSArray *callbackBlocks = [self.callbackBlocks copy]; UNLOCK(self.callbacksLock); - // We need to remove [NSNull null] because there might not always be a progress block for each callback - [callbacks removeObjectIdenticalTo:[NSNull null]]; + NSMutableArray *callbacks = [NSMutableArray arrayWithCapacity:callbackBlocks.count]; + for (SDCallbacksDictionary *callbacksDic in callbackBlocks) { + id callback = callbacksDic[key]; + if (callback) { + [callbacks addObject:callback]; + } + } return [callbacks copy]; // strip mutability here } diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index a7238e83..33f5f6c6 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -189,7 +189,7 @@ static id _defaultImageLoader; // Check whether we should query cache BOOL shouldQueryCache = (options & SDWebImageFromLoaderOnly) == 0; if (shouldQueryCache) { - id cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; + id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; __weak SDWebImageCombinedOperation *weakOperation = operation; operation.cacheOperation = [self.imageCache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) { @@ -277,13 +277,13 @@ static id _defaultImageLoader; } SDImageCacheType storeCacheType = SDImageCacheTypeAll; - if ([context valueForKey:SDWebImageContextStoreCacheType]) { - storeCacheType = [[context valueForKey:SDWebImageContextStoreCacheType] unsignedIntegerValue]; + if (context[SDWebImageContextStoreCacheType]) { + storeCacheType = [context[SDWebImageContextStoreCacheType] integerValue]; } - id cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; + id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; - id transformer = [context valueForKey:SDWebImageContextImageTransformer]; - id cacheSerializer = [context valueForKey:SDWebImageContextCacheKeyFilter]; + id transformer = context[SDWebImageContextImageTransformer]; + id cacheSerializer = context[SDWebImageContextCacheKeyFilter]; if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key]; @@ -369,17 +369,17 @@ static id _defaultImageLoader; SDWebImageMutableContext *mutableContext = [SDWebImageMutableContext dictionary]; // Image Transformer from manager - if (![context valueForKey:SDWebImageContextImageTransformer]) { + if (!context[SDWebImageContextImageTransformer]) { id transformer = self.transformer; [mutableContext setValue:transformer forKey:SDWebImageContextImageTransformer]; } // Cache key filter from manager - if (![context valueForKey:SDWebImageContextCacheKeyFilter]) { + if (!context[SDWebImageContextCacheKeyFilter]) { id cacheKeyFilter = self.cacheKeyFilter; [mutableContext setValue:cacheKeyFilter forKey:SDWebImageContextCacheKeyFilter]; } // Cache serializer from manager - if (![context valueForKey:SDWebImageContextCacheSerializer]) { + if (!context[SDWebImageContextCacheSerializer]) { id cacheSerializer = self.cacheSerializer; [mutableContext setValue:cacheSerializer forKey:SDWebImageContextCacheSerializer]; } diff --git a/SDWebImage/UIView+WebCache.m b/SDWebImage/UIView+WebCache.m index 8a73d740..d7e73cd8 100644 --- a/SDWebImage/UIView+WebCache.m +++ b/SDWebImage/UIView+WebCache.m @@ -44,10 +44,8 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; progress:(nullable SDImageLoaderProgressBlock)progressBlock completed:(nullable SDInternalCompletionBlock)completedBlock { context = [context copy]; // copy to avoid mutable object - NSString *validOperationKey = nil; - if ([context valueForKey:SDWebImageContextSetImageOperationKey]) { - validOperationKey = [context valueForKey:SDWebImageContextSetImageOperationKey]; - } else { + NSString *validOperationKey = context[SDWebImageContextSetImageOperationKey]; + if (!validOperationKey) { validOperationKey = NSStringFromClass([self class]); } [self sd_cancelImageLoadOperationWithKey:validOperationKey]; @@ -70,10 +68,8 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; id imageIndicator = self.sd_imageIndicator; #endif - SDWebImageManager *manager; - if ([context valueForKey:SDWebImageContextCustomManager]) { - manager = (SDWebImageManager *)[context valueForKey:SDWebImageContextCustomManager]; - } else { + SDWebImageManager *manager = context[SDWebImageContextCustomManager]; + if (!manager) { manager = [SDWebImageManager sharedManager]; } diff --git a/SDWebImage/WebP/SDImageWebPCoder.m b/SDWebImage/WebP/SDImageWebPCoder.m index 83f641c4..56f6e6b4 100644 --- a/SDWebImage/WebP/SDImageWebPCoder.m +++ b/SDWebImage/WebP/SDImageWebPCoder.m @@ -112,10 +112,11 @@ uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); BOOL hasAnimation = flags & ANIMATION_FLAG; - BOOL decodeFirstFrame = [[options valueForKey:SDImageCoderDecodeFirstFrameOnly] boolValue]; + BOOL decodeFirstFrame = [options[SDImageCoderDecodeFirstFrameOnly] boolValue]; CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -212,8 +213,9 @@ // Progressive images need transparent, so always use premultiplied BGRA _idec = WebPINewRGB(MODE_bgrA, NULL, 0, 0); CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -284,8 +286,9 @@ return nil; } CGFloat scale = _scale; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } @@ -425,8 +428,8 @@ NSData *data; double compressionQuality = 1; - if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { - compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; + if (options[SDImageCoderEncodeCompressionQuality]) { + compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } NSArray *frames = [SDImageCoderHelper framesFromAnimatedImage:image]; @@ -633,8 +636,9 @@ static void FreeImageData(void *info, const void *data, size_t size) { return nil; } CGFloat scale = 1; - if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { - scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; + NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; + if (scaleFactor != nil) { + scale = [scaleFactor doubleValue]; if (scale < 1) { scale = 1; } From 532a4d82294b2317fb947f7fe2b03acbaf22f8f3 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 24 Jul 2018 10:09:57 +0800 Subject: [PATCH 2/4] Replace setValue:forKey with syntactic sugar --- SDWebImage/SDImageAPNGCoder.m | 4 ++-- SDWebImage/SDImageGIFCoder.m | 4 ++-- SDWebImage/SDImageIOCoder.m | 4 ++-- SDWebImage/SDWebImageManager.m | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SDWebImage/SDImageAPNGCoder.m b/SDWebImage/SDImageAPNGCoder.m index a7cea27b..8c58aa1b 100644 --- a/SDWebImage/SDImageAPNGCoder.m +++ b/SDWebImage/SDImageAPNGCoder.m @@ -203,7 +203,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef if (options[SDImageCoderEncodeCompressionQuality]) { compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } - [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; + properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality); BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; if (encodeFirstFrame || frames.count == 0) { @@ -213,7 +213,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef // for animated APNG images NSUInteger loopCount = image.sd_imageLoopCount; NSDictionary *pngProperties = @{(__bridge NSString *)kCGImagePropertyAPNGLoopCount : @(loopCount)}; - [properties setValue:pngProperties forKey:(__bridge NSString *)kCGImagePropertyPNGDictionary]; + properties[(__bridge NSString *)kCGImagePropertyPNGDictionary] = pngProperties; CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties); for (size_t i = 0; i < frames.count; i++) { diff --git a/SDWebImage/SDImageGIFCoder.m b/SDWebImage/SDImageGIFCoder.m index 41cd4e00..008a2b0b 100644 --- a/SDWebImage/SDImageGIFCoder.m +++ b/SDWebImage/SDImageGIFCoder.m @@ -288,7 +288,7 @@ if (options[SDImageCoderEncodeCompressionQuality]) { compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } - [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; + properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality); BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; if (encodeFirstFrame || frames.count == 0) { @@ -298,7 +298,7 @@ // for animated GIF images NSUInteger loopCount = image.sd_imageLoopCount; NSDictionary *gifProperties = @{(__bridge NSString *)kCGImagePropertyGIFLoopCount : @(loopCount)}; - [properties setValue:gifProperties forKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]; + properties[(__bridge NSString *)kCGImagePropertyGIFDictionary] = gifProperties; CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties); for (size_t i = 0; i < frames.count; i++) { diff --git a/SDWebImage/SDImageIOCoder.m b/SDWebImage/SDImageIOCoder.m index 7a34ee8e..a103a265 100644 --- a/SDWebImage/SDImageIOCoder.m +++ b/SDWebImage/SDImageIOCoder.m @@ -221,12 +221,12 @@ #else CGImagePropertyOrientation exifOrientation = kCGImagePropertyOrientationUp; #endif - [properties setValue:@(exifOrientation) forKey:(__bridge NSString *)kCGImagePropertyOrientation]; + properties[(__bridge NSString *)kCGImagePropertyOrientation] = @(exifOrientation); double compressionQuality = 1; if (options[SDImageCoderEncodeCompressionQuality]) { compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue]; } - [properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; + properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality); // Add your image to the destination. CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties); diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 33f5f6c6..bddb4c85 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -233,7 +233,7 @@ static id _defaultImageLoader; } else { mutableContext = [NSMutableDictionary dictionary]; } - [mutableContext setValue:cachedImage forKey:SDWebImageContextLoaderCachedImage]; + mutableContext[SDWebImageContextLoaderCachedImage] = cachedImage; context = [mutableContext copy]; } From 2b7b5f5fa2fe3f7966b527ff60cd5cc6ef0efa41 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 24 Jul 2018 13:05:12 +0800 Subject: [PATCH 3/4] Use NSDictionary syntatic sugar --- SDWebImage/SDWebImageDownloader.m | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index f5f638d8..dfc39c57 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -134,11 +134,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; return; } NSMutableDictionary *mutableHTTPHeaders = [self.HTTPHeaders mutableCopy]; - if (value) { - [mutableHTTPHeaders setObject:value forKey:field]; - } else { - [mutableHTTPHeaders removeObjectForKey:field]; - } + [mutableHTTPHeaders setValue:value forKey:field]; self.HTTPHeaders = [mutableHTTPHeaders copy]; } @@ -272,7 +268,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext; [sself.URLOperations removeObjectForKey:url]; UNLOCK(sself.operationsLock); }; - [self.URLOperations setObject:operation forKey:url]; + self.URLOperations[url] = operation; // 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.downloadQueue addOperation:operation]; From 30d077b0a649fabccd4324ba219250c2ddb96cae Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 10 Aug 2018 22:53:13 +0800 Subject: [PATCH 4/4] Make callbacks of download more readable --- SDWebImage/SDWebImageDownloaderOperation.m | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 2690beab..a6c13fa7 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -105,15 +105,10 @@ typedef NSMutableDictionary SDCallbacksDictionary; - (nullable NSArray *)callbacksForKey:(NSString *)key { LOCK(self.callbacksLock); - NSArray *callbackBlocks = [self.callbackBlocks copy]; + NSMutableArray *callbacks = [[self.callbackBlocks valueForKey:key] mutableCopy]; UNLOCK(self.callbacksLock); - NSMutableArray *callbacks = [NSMutableArray arrayWithCapacity:callbackBlocks.count]; - for (SDCallbacksDictionary *callbacksDic in callbackBlocks) { - id callback = callbacksDic[key]; - if (callback) { - [callbacks addObject:callback]; - } - } + // We need to remove [NSNull null] because there might not always be a progress block for each callback + [callbacks removeObjectIdenticalTo:[NSNull null]]; return [callbacks copy]; // strip mutability here }