Merge pull request #2399 from zhongwuzw/replace-valueforkey

Replace valueForKey with objectForKey when access NSDictionary
This commit is contained in:
Bogdan Poplauschi 2018-08-10 18:19:59 +03:00 committed by GitHub
commit 5ea8074bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 102 additions and 103 deletions

View File

@ -81,8 +81,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -139,9 +140,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
- (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source { - (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source {
NSUInteger loopCount = 0; NSUInteger loopCount = 0;
NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil); NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
NSDictionary *pngProperties = [imageProperties valueForKey:(__bridge NSString *)kCGImagePropertyPNGDictionary]; NSDictionary *pngProperties = imageProperties[(__bridge NSString *)kCGImagePropertyPNGDictionary];
if (pngProperties) { if (pngProperties) {
NSNumber *apngLoopCount = [pngProperties valueForKey:(__bridge NSString *)kCGImagePropertyAPNGLoopCount]; NSNumber *apngLoopCount = pngProperties[(__bridge NSString *)kCGImagePropertyAPNGLoopCount];
if (apngLoopCount != nil) { if (apngLoopCount != nil) {
loopCount = apngLoopCount.unsignedIntegerValue; loopCount = apngLoopCount.unsignedIntegerValue;
} }
@ -199,10 +200,10 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
} }
NSMutableDictionary *properties = [NSMutableDictionary dictionary]; NSMutableDictionary *properties = [NSMutableDictionary dictionary];
double compressionQuality = 1; double compressionQuality = 1;
if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { if (options[SDImageCoderEncodeCompressionQuality]) {
compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
} }
[properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
if (encodeFirstFrame || frames.count == 0) { if (encodeFirstFrame || frames.count == 0) {
@ -212,7 +213,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
// for animated APNG images // for animated APNG images
NSUInteger loopCount = image.sd_imageLoopCount; NSUInteger loopCount = image.sd_imageLoopCount;
NSDictionary *pngProperties = @{(__bridge NSString *)kCGImagePropertyAPNGLoopCount : @(loopCount)}; NSDictionary *pngProperties = @{(__bridge NSString *)kCGImagePropertyAPNGLoopCount : @(loopCount)};
[properties setValue:pngProperties forKey:(__bridge NSString *)kCGImagePropertyPNGDictionary]; properties[(__bridge NSString *)kCGImagePropertyPNGDictionary] = pngProperties;
CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties); CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties);
for (size_t i = 0; i < frames.count; i++) { for (size_t i = 0; i < frames.count; i++) {
@ -246,8 +247,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatPNG]; CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatPNG];
_imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType}); _imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType});
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -297,8 +299,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
if (partialImageRef) { if (partialImageRef) {
CGFloat scale = _scale; CGFloat scale = _scale;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -334,8 +337,9 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }

View File

@ -371,9 +371,9 @@
return nil; return nil;
} }
if ([context valueForKey:SDWebImageContextImageTransformer]) { id<SDImageTransformer> transformer = context[SDWebImageContextImageTransformer];
if (transformer) {
// grab the transformed disk image if transformer provided // grab the transformed disk image if transformer provided
id<SDImageTransformer> transformer = [context valueForKey:SDWebImageContextImageTransformer];
NSString *transformerKey = [transformer transformerKey]; NSString *transformerKey = [transformer transformerKey];
key = SDTransformedKeyForKey(key, transformerKey); key = SDTransformedKeyForKey(key, transformerKey);
} }

View File

@ -15,20 +15,18 @@
UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) { UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
UIImage *image; UIImage *image;
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor];
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
if (!decodeFirstFrame) { if (!decodeFirstFrame) {
Class animatedImageClass = context[SDWebImageContextAnimatedImageClass];
// check whether we should use `SDAnimatedImage` // check whether we should use `SDAnimatedImage`
if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) {
Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; image = [[animatedImageClass alloc] initWithData:imageData scale:scale];
if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) {
image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; [((id<SDAnimatedImage>)image) preloadAllFrames];
if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) {
[((id<SDAnimatedImage>)image) preloadAllFrames];
}
} }
} }
} }

View File

@ -74,8 +74,9 @@
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -132,9 +133,9 @@
- (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source { - (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source {
NSUInteger loopCount = 1; NSUInteger loopCount = 1;
NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil); NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
NSDictionary *gifProperties = [imageProperties valueForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]; NSDictionary *gifProperties = imageProperties[(__bridge NSString *)kCGImagePropertyGIFDictionary];
if (gifProperties) { if (gifProperties) {
NSNumber *gifLoopCount = [gifProperties valueForKey:(__bridge NSString *)kCGImagePropertyGIFLoopCount]; NSNumber *gifLoopCount = gifProperties[(__bridge NSString *)kCGImagePropertyGIFLoopCount];
if (gifLoopCount != nil) { if (gifLoopCount != nil) {
loopCount = gifLoopCount.unsignedIntegerValue; loopCount = gifLoopCount.unsignedIntegerValue;
} }
@ -186,8 +187,9 @@
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF]; CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF];
_imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType}); _imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType});
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -237,8 +239,9 @@
if (partialImageRef) { if (partialImageRef) {
CGFloat scale = _scale; CGFloat scale = _scale;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -282,10 +285,10 @@
} }
NSMutableDictionary *properties = [NSMutableDictionary dictionary]; NSMutableDictionary *properties = [NSMutableDictionary dictionary];
double compressionQuality = 1; double compressionQuality = 1;
if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { if (options[SDImageCoderEncodeCompressionQuality]) {
compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
} }
[properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
if (encodeFirstFrame || frames.count == 0) { if (encodeFirstFrame || frames.count == 0) {
@ -295,7 +298,7 @@
// for animated GIF images // for animated GIF images
NSUInteger loopCount = image.sd_imageLoopCount; NSUInteger loopCount = image.sd_imageLoopCount;
NSDictionary *gifProperties = @{(__bridge NSString *)kCGImagePropertyGIFLoopCount : @(loopCount)}; NSDictionary *gifProperties = @{(__bridge NSString *)kCGImagePropertyGIFLoopCount : @(loopCount)};
[properties setValue:gifProperties forKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]; properties[(__bridge NSString *)kCGImagePropertyGIFDictionary] = gifProperties;
CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties); CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties);
for (size_t i = 0; i < frames.count; i++) { for (size_t i = 0; i < frames.count; i++) {
@ -335,8 +338,9 @@
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }

View File

@ -68,8 +68,9 @@
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -103,8 +104,9 @@
if (self) { if (self) {
_imageSource = CGImageSourceCreateIncremental(NULL); _imageSource = CGImageSourceCreateIncremental(NULL);
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -159,8 +161,9 @@
if (partialImageRef) { if (partialImageRef) {
CGFloat scale = _scale; CGFloat scale = _scale;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -227,12 +230,12 @@
#else #else
CGImagePropertyOrientation exifOrientation = kCGImagePropertyOrientationUp; CGImagePropertyOrientation exifOrientation = kCGImagePropertyOrientationUp;
#endif #endif
[properties setValue:@(exifOrientation) forKey:(__bridge NSString *)kCGImagePropertyOrientation]; properties[(__bridge NSString *)kCGImagePropertyOrientation] = @(exifOrientation);
double compressionQuality = 1; double compressionQuality = 1;
if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { if (options[SDImageCoderEncodeCompressionQuality]) {
compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
} }
[properties setValue:@(compressionQuality) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality]; properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
// Add your image to the destination. // Add your image to the destination.
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties); CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties);

View File

@ -21,7 +21,7 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS
NSCParameterAssert(imageURL); NSCParameterAssert(imageURL);
UIImage *image; UIImage *image;
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
NSString *cacheKey; NSString *cacheKey;
if (cacheKeyFilter) { if (cacheKeyFilter) {
cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL]; cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL];
@ -29,20 +29,18 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS
cacheKey = imageURL.absoluteString; cacheKey = imageURL.absoluteString;
} }
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor];
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
if (!decodeFirstFrame) { if (!decodeFirstFrame) {
// check whether we should use `SDAnimatedImage` // check whether we should use `SDAnimatedImage`
if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { Class animatedImageClass = context[SDWebImageContextAnimatedImageClass];
Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) {
if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) { image = [[animatedImageClass alloc] initWithData:imageData scale:scale];
image = [[animatedImageClass alloc] initWithData:imageData scale:scale]; if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) {
if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) { [((id<SDAnimatedImage>)image) preloadAllFrames];
[((id<SDAnimatedImage>)image) preloadAllFrames];
}
} }
} }
} }
@ -84,7 +82,7 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im
NSCParameterAssert(operation); NSCParameterAssert(operation);
UIImage *image; UIImage *image;
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
NSString *cacheKey; NSString *cacheKey;
if (cacheKeyFilter) { if (cacheKeyFilter) {
cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL]; cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL];
@ -92,7 +90,7 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im
cacheKey = imageURL.absoluteString; cacheKey = imageURL.absoluteString;
} }
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor]; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor];
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
@ -117,11 +115,9 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im
[progressiveCoder updateIncrementalData:imageData finished:finished]; [progressiveCoder updateIncrementalData:imageData finished:finished];
if (!decodeFirstFrame) { if (!decodeFirstFrame) {
// check whether we should use `SDAnimatedImage` // check whether we should use `SDAnimatedImage`
if ([context valueForKey:SDWebImageContextAnimatedImageClass]) { Class animatedImageClass = context[SDWebImageContextAnimatedImageClass];
Class animatedImageClass = [context valueForKey:SDWebImageContextAnimatedImageClass]; if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)] && [progressiveCoder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) {
if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)] && [progressiveCoder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) { image = [[animatedImageClass alloc] initWithAnimatedCoder:(id<SDAnimatedImageCoder>)progressiveCoder scale:scale];
image = [[animatedImageClass alloc] initWithAnimatedCoder:(id<SDAnimatedImageCoder>)progressiveCoder scale:scale];
}
} }
} }
if (!image) { if (!image) {

View File

@ -159,11 +159,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
return; return;
} }
NSMutableDictionary *mutableHTTPHeaders = [self.HTTPHeaders mutableCopy]; NSMutableDictionary *mutableHTTPHeaders = [self.HTTPHeaders mutableCopy];
if (value) { [mutableHTTPHeaders setValue:value forKey:field];
[mutableHTTPHeaders setObject:value forKey:field];
} else {
[mutableHTTPHeaders removeObjectForKey:field];
}
self.HTTPHeaders = [mutableHTTPHeaders copy]; self.HTTPHeaders = [mutableHTTPHeaders copy];
} }
@ -215,7 +211,7 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
[sself.URLOperations removeObjectForKey:url]; [sself.URLOperations removeObjectForKey:url];
UNLOCK(sself.operationsLock); 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. // 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. // `addOperation:` does not synchronously execute the `operation.completionBlock` so this will not cause deadlock.
[self.downloadQueue addOperation:operation]; [self.downloadQueue addOperation:operation];
@ -502,10 +498,8 @@ didReceiveResponse:(NSURLResponse *)response
} }
- (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock { - (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
UIImage *cachedImage; UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage];
if ([context valueForKey:SDWebImageContextLoaderCachedImage]) {
cachedImage = [context valueForKey:SDWebImageContextLoaderCachedImage];
}
SDWebImageDownloaderOptions downloaderOptions = 0; SDWebImageDownloaderOptions downloaderOptions = 0;
if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority; if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
if (options & SDWebImageProgressiveLoad) downloaderOptions |= SDWebImageDownloaderProgressiveLoad; if (options & SDWebImageProgressiveLoad) downloaderOptions |= SDWebImageDownloaderProgressiveLoad;

View File

@ -189,7 +189,7 @@ static id<SDImageLoader> _defaultImageLoader;
// Check whether we should query cache // Check whether we should query cache
BOOL shouldQueryCache = (options & SDWebImageFromLoaderOnly) == 0; BOOL shouldQueryCache = (options & SDWebImageFromLoaderOnly) == 0;
if (shouldQueryCache) { if (shouldQueryCache) {
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
__weak SDWebImageCombinedOperation *weakOperation = operation; __weak SDWebImageCombinedOperation *weakOperation = operation;
operation.cacheOperation = [self.imageCache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) { operation.cacheOperation = [self.imageCache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) {
@ -233,7 +233,7 @@ static id<SDImageLoader> _defaultImageLoader;
} else { } else {
mutableContext = [NSMutableDictionary dictionary]; mutableContext = [NSMutableDictionary dictionary];
} }
[mutableContext setValue:cachedImage forKey:SDWebImageContextLoaderCachedImage]; mutableContext[SDWebImageContextLoaderCachedImage] = cachedImage;
context = [mutableContext copy]; context = [mutableContext copy];
} }
@ -277,13 +277,13 @@ static id<SDImageLoader> _defaultImageLoader;
} }
SDImageCacheType storeCacheType = SDImageCacheTypeAll; SDImageCacheType storeCacheType = SDImageCacheTypeAll;
if ([context valueForKey:SDWebImageContextStoreCacheType]) { if (context[SDWebImageContextStoreCacheType]) {
storeCacheType = [[context valueForKey:SDWebImageContextStoreCacheType] unsignedIntegerValue]; storeCacheType = [context[SDWebImageContextStoreCacheType] integerValue];
} }
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter]; id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
id<SDImageTransformer> transformer = [context valueForKey:SDWebImageContextImageTransformer]; id<SDImageTransformer> transformer = context[SDWebImageContextImageTransformer];
id<SDWebImageCacheSerializer> cacheSerializer = [context valueForKey:SDWebImageContextCacheKeyFilter]; id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheKeyFilter];
if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) { if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key]; UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];
@ -369,17 +369,17 @@ static id<SDImageLoader> _defaultImageLoader;
SDWebImageMutableContext *mutableContext = [SDWebImageMutableContext dictionary]; SDWebImageMutableContext *mutableContext = [SDWebImageMutableContext dictionary];
// Image Transformer from manager // Image Transformer from manager
if (![context valueForKey:SDWebImageContextImageTransformer]) { if (!context[SDWebImageContextImageTransformer]) {
id<SDImageTransformer> transformer = self.transformer; id<SDImageTransformer> transformer = self.transformer;
[mutableContext setValue:transformer forKey:SDWebImageContextImageTransformer]; [mutableContext setValue:transformer forKey:SDWebImageContextImageTransformer];
} }
// Cache key filter from manager // Cache key filter from manager
if (![context valueForKey:SDWebImageContextCacheKeyFilter]) { if (!context[SDWebImageContextCacheKeyFilter]) {
id<SDWebImageCacheKeyFilter> cacheKeyFilter = self.cacheKeyFilter; id<SDWebImageCacheKeyFilter> cacheKeyFilter = self.cacheKeyFilter;
[mutableContext setValue:cacheKeyFilter forKey:SDWebImageContextCacheKeyFilter]; [mutableContext setValue:cacheKeyFilter forKey:SDWebImageContextCacheKeyFilter];
} }
// Cache serializer from manager // Cache serializer from manager
if (![context valueForKey:SDWebImageContextCacheSerializer]) { if (!context[SDWebImageContextCacheSerializer]) {
id<SDWebImageCacheSerializer> cacheSerializer = self.cacheSerializer; id<SDWebImageCacheSerializer> cacheSerializer = self.cacheSerializer;
[mutableContext setValue:cacheSerializer forKey:SDWebImageContextCacheSerializer]; [mutableContext setValue:cacheSerializer forKey:SDWebImageContextCacheSerializer];
} }

View File

@ -44,10 +44,8 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
progress:(nullable SDImageLoaderProgressBlock)progressBlock progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDInternalCompletionBlock)completedBlock { completed:(nullable SDInternalCompletionBlock)completedBlock {
context = [context copy]; // copy to avoid mutable object context = [context copy]; // copy to avoid mutable object
NSString *validOperationKey = nil; NSString *validOperationKey = context[SDWebImageContextSetImageOperationKey];
if ([context valueForKey:SDWebImageContextSetImageOperationKey]) { if (!validOperationKey) {
validOperationKey = [context valueForKey:SDWebImageContextSetImageOperationKey];
} else {
validOperationKey = NSStringFromClass([self class]); validOperationKey = NSStringFromClass([self class]);
} }
[self sd_cancelImageLoadOperationWithKey:validOperationKey]; [self sd_cancelImageLoadOperationWithKey:validOperationKey];
@ -70,10 +68,8 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
id<SDWebImageIndicator> imageIndicator = self.sd_imageIndicator; id<SDWebImageIndicator> imageIndicator = self.sd_imageIndicator;
#endif #endif
SDWebImageManager *manager; SDWebImageManager *manager = context[SDWebImageContextCustomManager];
if ([context valueForKey:SDWebImageContextCustomManager]) { if (!manager) {
manager = (SDWebImageManager *)[context valueForKey:SDWebImageContextCustomManager];
} else {
manager = [SDWebImageManager sharedManager]; manager = [SDWebImageManager sharedManager];
} }

View File

@ -112,10 +112,11 @@
uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS);
BOOL hasAnimation = flags & ANIMATION_FLAG; BOOL hasAnimation = flags & ANIMATION_FLAG;
BOOL decodeFirstFrame = [[options valueForKey:SDImageCoderDecodeFirstFrameOnly] boolValue]; BOOL decodeFirstFrame = [options[SDImageCoderDecodeFirstFrameOnly] boolValue];
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -212,8 +213,9 @@
// Progressive images need transparent, so always use premultiplied BGRA // Progressive images need transparent, so always use premultiplied BGRA
_idec = WebPINewRGB(MODE_bgrA, NULL, 0, 0); _idec = WebPINewRGB(MODE_bgrA, NULL, 0, 0);
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -284,8 +286,9 @@
return nil; return nil;
} }
CGFloat scale = _scale; CGFloat scale = _scale;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }
@ -425,8 +428,8 @@
NSData *data; NSData *data;
double compressionQuality = 1; double compressionQuality = 1;
if ([options valueForKey:SDImageCoderEncodeCompressionQuality]) { if (options[SDImageCoderEncodeCompressionQuality]) {
compressionQuality = [[options valueForKey:SDImageCoderEncodeCompressionQuality] doubleValue]; compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
} }
NSArray<SDImageFrame *> *frames = [SDImageCoderHelper framesFromAnimatedImage:image]; NSArray<SDImageFrame *> *frames = [SDImageCoderHelper framesFromAnimatedImage:image];
@ -633,8 +636,9 @@ static void FreeImageData(void *info, const void *data, size_t size) {
return nil; return nil;
} }
CGFloat scale = 1; CGFloat scale = 1;
if ([options valueForKey:SDImageCoderDecodeScaleFactor]) { NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
scale = [[options valueForKey:SDImageCoderDecodeScaleFactor] doubleValue]; if (scaleFactor != nil) {
scale = [scaleFactor doubleValue];
if (scale < 1) { if (scale < 1) {
scale = 1; scale = 1;
} }