From 991fd899854340ed18a8fc871cebd23b5372f776 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 12 Sep 2018 23:04:45 +0800 Subject: [PATCH 01/15] Fix memory cache image cost miscalculation for 4.x --- SDWebImage/SDImageCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 75d44f02..6be1e3fa 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -18,7 +18,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { #if SD_MAC return image.size.height * image.size.width; #elif SD_UIKIT || SD_WATCH - return image.size.height * image.size.width * image.scale * image.scale; + return image.size.height * image.size.width * image.scale * image.scale * CGImageGetBitsPerPixel(image.CGImage) / 8; #endif } From 504dc10491553c70be99480ba995362fc40b9218 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 13 Sep 2018 10:48:52 +0800 Subject: [PATCH 02/15] Take images into account --- SDWebImage/SDImageCache.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 6be1e3fa..6dc6150a 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -18,7 +18,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { #if SD_MAC return image.size.height * image.size.width; #elif SD_UIKIT || SD_WATCH - return image.size.height * image.size.width * image.scale * image.scale * CGImageGetBitsPerPixel(image.CGImage) / 8; + NSUInteger imageSize = image.size.height * image.size.width * image.scale * image.scale * CGImageGetBitsPerPixel(image.CGImage) / 8; + return image.images ? (imageSize * image.images.count) : imageSize; #endif } From 3fbb7856e5facb550b89398aa31364ce187557cd Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 11 Nov 2018 21:39:12 +0800 Subject: [PATCH 03/15] Transform protocol to category --- SDWebImage.xcodeproj/project.pbxproj | 28 +++++++++++++++ .../FLAnimatedImageView+WebCache.m | 16 +++++++++ SDWebImage/SDImageCache.m | 18 +++------- SDWebImage/UIImage+CacheMemoryCost.h | 21 +++++++++++ SDWebImage/UIImage+CacheMemoryCost.m | 36 +++++++++++++++++++ WebImage/SDWebImage.h | 1 + 6 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 SDWebImage/UIImage+CacheMemoryCost.h create mode 100644 SDWebImage/UIImage+CacheMemoryCost.m diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index 158e6699..e0ddba6b 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -37,6 +37,18 @@ 00733A711BC4880E00A5A117 /* UIImageView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D95148C56230056699D /* UIImageView+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; 00733A721BC4880E00A5A117 /* UIView+WebCacheOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 00733A731BC4880E00A5A117 /* SDWebImage.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A2CAE031AB4BB5400B6BC39 /* SDWebImage.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0A21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0B21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0C21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0D21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0E21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0F21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA1021983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA1121983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA1221983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA1321983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA1421983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA1521983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; 320224BB203979BA00E9F285 /* SDAnimatedImageRep.h in Headers */ = {isa = PBXBuildFile; fileRef = 320224B9203979BA00E9F285 /* SDAnimatedImageRep.h */; settings = {ATTRIBUTES = (Public, ); }; }; 320224BC203979BA00E9F285 /* SDAnimatedImageRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */; }; 321DB3612011D4D70015D2CB /* NSButton+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 321DB35F2011D4D60015D2CB /* NSButton+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -1289,6 +1301,8 @@ /* Begin PBXFileReference section */ 00733A4C1BC487C000A5A117 /* SDWebImage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImage.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+CacheMemoryCost.h"; sourceTree = ""; }; + 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+CacheMemoryCost.m"; sourceTree = ""; }; 320224B9203979BA00E9F285 /* SDAnimatedImageRep.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDAnimatedImageRep.h; sourceTree = ""; }; 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageRep.m; sourceTree = ""; }; 321DB35F2011D4D60015D2CB /* NSButton+WebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSButton+WebCache.h"; path = "SDWebImage/NSButton+WebCache.h"; sourceTree = ""; }; @@ -1790,6 +1804,8 @@ 321E60BD1F38E91700405457 /* UIImage+ForceDecode.m */, AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */, AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */, + 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */, + 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */, ); name = Categories; sourceTree = ""; @@ -2018,6 +2034,7 @@ 4369C27A1D9807EC007E863A /* UIView+WebCache.h in Headers */, 80377DCC1F2F66A700F89830 /* lossless_common.h in Headers */, 321E60971F38E8ED00405457 /* SDWebImageImageIOCoder.h in Headers */, + 0E9EFA0D21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 43A918671D8308FE00B3925F /* SDImageCacheConfig.h in Headers */, 431739571CDFC8B70008FEB9 /* encode.h in Headers */, 00733A6F1BC4880E00A5A117 /* UIImage+WebP.h in Headers */, @@ -2109,6 +2126,7 @@ 4314D16D1D0E0E3B004B36C9 /* SDImageCache.h in Headers */, 4314D16F1D0E0E3B004B36C9 /* NSData+ImageContentType.h in Headers */, 80377C121F2F666300F89830 /* bit_reader_inl_utils.h in Headers */, + 0E9EFA0B21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 4314D1701D0E0E3B004B36C9 /* mux.h in Headers */, 321E60871F38E8C800405457 /* SDWebImageCoder.h in Headers */, 80377EA21F2F66D400F89830 /* vp8i_dec.h in Headers */, @@ -2175,6 +2193,7 @@ 431BB6DC1D06D2C1006A3455 /* UIButton+WebCache.h in Headers */, 431BB6E11D06D2C1006A3455 /* SDWebImage.h in Headers */, 80377E311F2F66A800F89830 /* yuv.h in Headers */, + 0E9EFA0E21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 80377ECA1F2F66D500F89830 /* common_dec.h in Headers */, 80377C771F2F666400F89830 /* thread_utils.h in Headers */, 80377E1F1F2F66A800F89830 /* mips_macro.h in Headers */, @@ -2264,6 +2283,7 @@ 4397D2C81D0DDD8C00BB2784 /* SDWebImageCompat.h in Headers */, 4397D2CB1D0DDD8C00BB2784 /* UIImageView+HighlightedWebCache.h in Headers */, 4397D2CC1D0DDD8C00BB2784 /* mux.h in Headers */, + 0E9EFA0F21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 80377C911F2F666400F89830 /* thread_utils.h in Headers */, 4397D2D01D0DDD8C00BB2784 /* SDWebImageDownloaderOperation.h in Headers */, 4397D2D11D0DDD8C00BB2784 /* decode.h in Headers */, @@ -2334,6 +2354,7 @@ 80377D871F2F66A700F89830 /* lossless_common.h in Headers */, 321E60961F38E8ED00405457 /* SDWebImageImageIOCoder.h in Headers */, 4A2CAE041AB4BB5400B6BC39 /* SDWebImage.h in Headers */, + 0E9EFA0C21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 431739511CDFC8B70008FEB9 /* format_constants.h in Headers */, 43A918661D8308FE00B3925F /* SDImageCacheConfig.h in Headers */, 323F8B701F38EF770092B609 /* delta_palettization_enc.h in Headers */, @@ -2429,6 +2450,7 @@ 80377D0B1F2F66A100F89830 /* mips_macro.h in Headers */, 5376131A155AD0D5005750A4 /* SDWebImageDownloader.h in Headers */, 4369C2771D9807EC007E863A /* UIView+WebCache.h in Headers */, + 0E9EFA0A21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, 80377CEF1F2F66A100F89830 /* dsp.h in Headers */, 80377C011F2F665300F89830 /* filters_utils.h in Headers */, 5376131C155AD0D5005750A4 /* SDWebImageManager.h in Headers */, @@ -2698,6 +2720,7 @@ 323F8BBD1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0D1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377DBD1F2F66A700F89830 /* dec.c in Sources */, + 0E9EFA1321983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 00733A561BC4880000A5A117 /* SDWebImageDownloaderOperation.m in Sources */, 80377DE71F2F66A700F89830 /* upsampling.c in Sources */, 321E60C71F38E91700405457 /* UIImage+ForceDecode.m in Sources */, @@ -2972,6 +2995,7 @@ 321E60B71F38E90100405457 /* SDWebImageWebPCoder.m in Sources */, 80377D5D1F2F66A700F89830 /* upsampling.c in Sources */, 80377D251F2F66A700F89830 /* argb.c in Sources */, + 0E9EFA1121983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 80377D281F2F66A700F89830 /* cost_mips32.c in Sources */, 323F8BF11F38EF770092B609 /* anim_encode.c in Sources */, 4314D1551D0E0E3B004B36C9 /* UIImageView+HighlightedWebCache.m in Sources */, @@ -3116,6 +3140,7 @@ 321E60BA1F38E90100405457 /* SDWebImageWebPCoder.m in Sources */, 80377E2C1F2F66A800F89830 /* upsampling.c in Sources */, 80377DF41F2F66A800F89830 /* argb.c in Sources */, + 0E9EFA1421983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 80377DF71F2F66A800F89830 /* cost_mips32.c in Sources */, 323F8BF41F38EF770092B609 /* anim_encode.c in Sources */, 80377C6E1F2F666400F89830 /* quant_levels_dec_utils.c in Sources */, @@ -3134,6 +3159,7 @@ 80377E511F2F66A800F89830 /* filters_mips_dsp_r2.c in Sources */, 80377E371F2F66A800F89830 /* argb_mips_dsp_r2.c in Sources */, 80377E471F2F66A800F89830 /* dec.c in Sources */, + 0E9EFA1521983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 80377C921F2F666400F89830 /* utils.c in Sources */, 4397D27E1D0DDD8C00BB2784 /* UIImage+GIF.m in Sources */, 321E60911F38E8C800405457 /* SDWebImageCoder.m in Sources */, @@ -3282,6 +3308,7 @@ 323F8BBC1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0C1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377D781F2F66A700F89830 /* dec.c in Sources */, + 0E9EFA1221983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 80377DA21F2F66A700F89830 /* upsampling.c in Sources */, 80377C401F2F666300F89830 /* rescaler_utils.c in Sources */, 321E60C61F38E91700405457 /* UIImage+ForceDecode.m in Sources */, @@ -3430,6 +3457,7 @@ 323F8BBA1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0A1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377CEE1F2F66A100F89830 /* dec.c in Sources */, + 0E9EFA1021983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, 80377D181F2F66A100F89830 /* upsampling.c in Sources */, 80377C0C1F2F665300F89830 /* rescaler_utils.c in Sources */, 321E60C41F38E91700405457 /* UIImage+ForceDecode.m in Sources */, diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index 99c1d033..fec26e90 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -15,6 +15,13 @@ #import "NSData+ImageContentType.h" #import "UIImageView+WebCache.h" #import "UIImage+MultiFormat.h" +#import "UIImage+CacheMemoryCost.h" + +static inline NSUInteger SDWebImageCalculateFLAnimatedImageMemoryCost(FLAnimatedImage *image) { + UIImage *posterImage = image.posterImage; + // Calculate pixels value, we use pixels as cost to store into memory cache, so we also use image.data's bytes length divide 4. + return image.frameCacheSizeCurrent * posterImage.size.height * posterImage.size.width * posterImage.scale * posterImage.scale + image.data.length / 4; +} static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageView *imageView, NSData *imageData) { if ([NSData sd_imageFormatForImageData:imageData] != SDImageFormatGIF) { @@ -150,6 +157,15 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV if (animatedImage) { if (strongSelf.sd_cacheFLAnimatedImage) { image.sd_FLAnimatedImage = animatedImage; + image.sd_memoryCost = SDWebImageCalculateFLAnimatedImageMemoryCost(animatedImage); + NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; + UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:key]; + // If image can be find in memory cache, we update the cost value of cost. + if (cachedImage) { + [[SDImageCache sharedImageCache] removeImageForKey:key fromDisk:NO withCompletion:^{ + [[SDImageCache sharedImageCache] storeImage:image forKey:key toDisk:NO completion:nil]; + }]; + } } strongSelf.image = animatedImage.posterImage; strongSelf.animatedImage = animatedImage; diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 6dc6150a..e824f65a 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -9,20 +9,12 @@ #import "SDImageCache.h" #import #import "NSImage+WebCache.h" +#import "UIImage+CacheMemoryCost.h" #import "SDWebImageCodersManager.h" #define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); #define UNLOCK(lock) dispatch_semaphore_signal(lock); -FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { -#if SD_MAC - return image.size.height * image.size.width; -#elif SD_UIKIT || SD_WATCH - NSUInteger imageSize = image.size.height * image.size.width * image.scale * image.scale * CGImageGetBitsPerPixel(image.CGImage) / 8; - return image.images ? (imageSize * image.images.count) : imageSize; -#endif -} - // A memory cache which auto purge the cache on memory warning and support weak cache. @interface SDMemoryCache : NSCache @@ -100,7 +92,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // Sync cache NSUInteger cost = 0; if ([obj isKindOfClass:[UIImage class]]) { - cost = SDCacheCostForImage(obj); + cost = [(UIImage *)obj sd_memoryCost]; } [super setObject:obj forKey:key cost:cost]; } @@ -293,7 +285,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } // if memory cache is enabled if (self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDCacheCostForImage(image); + NSUInteger cost = image.sd_memoryCost; [self.memCache setObject:image forKey:key cost:cost]; } @@ -419,7 +411,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key { UIImage *diskImage = [self diskImageForKey:key]; if (diskImage && self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDCacheCostForImage(diskImage); + NSUInteger cost = diskImage.sd_memoryCost; [self.memCache setObject:diskImage forKey:key cost:cost]; } @@ -539,7 +531,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // decode image data only if in-memory cache missed diskImage = [self diskImageForKey:key data:diskData options:options]; if (diskImage && self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDCacheCostForImage(diskImage); + NSUInteger cost = diskImage.sd_memoryCost; [self.memCache setObject:diskImage forKey:key cost:cost]; } } diff --git a/SDWebImage/UIImage+CacheMemoryCost.h b/SDWebImage/UIImage+CacheMemoryCost.h new file mode 100644 index 00000000..67136d5e --- /dev/null +++ b/SDWebImage/UIImage+CacheMemoryCost.h @@ -0,0 +1,21 @@ +/* + * 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 + +@interface UIImage (CacheMemoryCost) + +/** + * The image memory cost calculation, this property would be used in memory cache of `SDImageCache`. + * The default value is pixels of `image` or `images`. + * If you set some associated object to `UIImage`, you can set the custom value to indicate the memory cost. + * If you set a new value after `UIImage` be cached to memory cache, you need to reinsert into cache with new value cost by yourself. + */ +@property (assign, nonatomic) NSUInteger sd_memoryCost; + +@end diff --git a/SDWebImage/UIImage+CacheMemoryCost.m b/SDWebImage/UIImage+CacheMemoryCost.m new file mode 100644 index 00000000..48462dd5 --- /dev/null +++ b/SDWebImage/UIImage+CacheMemoryCost.m @@ -0,0 +1,36 @@ +/* + * 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 "UIImage+CacheMemoryCost.h" +#import "SDWebImageCompat.h" +#import "objc/runtime.h" + +FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { +#if SD_MAC + return image.size.height * image.size.width; +#elif SD_UIKIT || SD_WATCH + NSUInteger imageSize = image.size.height * image.size.width * image.scale * image.scale; + return image.images ? (imageSize * image.images.count) : imageSize; +#endif +} + +@implementation UIImage (CacheMemoryCost) + +- (NSUInteger)sd_memoryCost { + NSNumber *memoryCost = objc_getAssociatedObject(self, _cmd); + if (memoryCost == nil) { + memoryCost = @(SDCacheCostForImage(self)); + } + return [memoryCost unsignedIntegerValue]; +} + +- (void)setSd_memoryCost:(NSUInteger)sd_memoryCost { + objc_setAssociatedObject(self, @selector(sd_memoryCost), @(sd_memoryCost), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end diff --git a/WebImage/SDWebImage.h b/WebImage/SDWebImage.h index 07c173e3..cdf3109f 100644 --- a/WebImage/SDWebImage.h +++ b/WebImage/SDWebImage.h @@ -32,6 +32,7 @@ FOUNDATION_EXPORT const unsigned char WebImageVersionString[]; #import #import #import +#import #import #import #import From 62ce1d3b5e34552f18d3b4ecf67381aae100bc14 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 11 Nov 2018 22:16:03 +0800 Subject: [PATCH 04/15] Fix macos build failed --- SDWebImage/UIImage+CacheMemoryCost.h | 2 +- SDWebImage/UIImage+CacheMemoryCost.m | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/SDWebImage/UIImage+CacheMemoryCost.h b/SDWebImage/UIImage+CacheMemoryCost.h index 67136d5e..480230e9 100644 --- a/SDWebImage/UIImage+CacheMemoryCost.h +++ b/SDWebImage/UIImage+CacheMemoryCost.h @@ -6,7 +6,7 @@ * file that was distributed with this source code. */ -#import +#import "SDWebImageCompat.h" @interface UIImage (CacheMemoryCost) diff --git a/SDWebImage/UIImage+CacheMemoryCost.m b/SDWebImage/UIImage+CacheMemoryCost.m index 48462dd5..be92eb4b 100644 --- a/SDWebImage/UIImage+CacheMemoryCost.m +++ b/SDWebImage/UIImage+CacheMemoryCost.m @@ -7,7 +7,6 @@ */ #import "UIImage+CacheMemoryCost.h" -#import "SDWebImageCompat.h" #import "objc/runtime.h" FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { From f47d60a80257fe959d61e11aae6d9b7167b8bd61 Mon Sep 17 00:00:00 2001 From: langyanduan Date: Thu, 29 Nov 2018 11:10:51 +0800 Subject: [PATCH 05/15] fix: FLAnimatedImage custom property --- SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index 84a278b2..fc39bafb 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -22,7 +22,7 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV } FLAnimatedImage *animatedImage; // Compatibility in 4.x for lower version FLAnimatedImage. - if ([FLAnimatedImage respondsToSelector:@selector(initWithAnimatedGIFData:optimalFrameCacheSize:predrawingEnabled:)]) { + if ([FLAnimatedImage instancesRespondToSelector:@selector(initWithAnimatedGIFData:optimalFrameCacheSize:predrawingEnabled:)]) { animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData optimalFrameCacheSize:imageView.sd_optimalFrameCacheSize predrawingEnabled:imageView.sd_predrawingEnabled]; } else { animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData]; From 1b1cf603ed2012fd64fa272e26f342749c6c5d91 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 1 Dec 2018 14:56:31 +0800 Subject: [PATCH 06/15] Revert "Add image url check when set image" --- SDWebImage/UIView+WebCache.m | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/SDWebImage/UIView+WebCache.m b/SDWebImage/UIView+WebCache.m index d88cb39f..112a5e1d 100644 --- a/SDWebImage/UIView+WebCache.m +++ b/SDWebImage/UIView+WebCache.m @@ -101,7 +101,7 @@ static char TAG_ACTIVITY_SHOW; }; id operation = [manager loadImageWithURL:url options:options progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { __strong __typeof (wself) sself = wself; - if (!sself || ![imageURL isEqual:sself.sd_imageURL]) { return; } + if (!sself) { return; } #if SD_UIKIT [sself sd_removeActivityIndicator]; #endif @@ -114,10 +114,9 @@ static char TAG_ACTIVITY_SHOW; BOOL shouldNotSetImage = ((image && (options & SDWebImageAvoidAutoSetImage)) || (!image && !(options & SDWebImageDelayPlaceholder))); SDWebImageNoParamsBlock callCompletedBlockClojure = ^{ - __strong __typeof(wself) strongSelf = wself; - if (!strongSelf || ![strongSelf.sd_imageURL isEqual:imageURL]) { return; } + if (!sself) { return; } if (!shouldNotSetImage) { - [strongSelf sd_setNeedsLayout]; + [sself sd_setNeedsLayout]; } if (completedBlock && shouldCallCompletedBlock) { completedBlock(image, error, cacheType, url); @@ -155,7 +154,6 @@ static char TAG_ACTIVITY_SHOW; if (group) { dispatch_group_enter(group); } - if (![sself.sd_imageURL isEqual:imageURL]) { return ; } #if SD_UIKIT || SD_MAC [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL]; #else From 72a34cf2cf0a42009676018e75b6657e00dd271c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 3 Dec 2018 10:58:24 +0800 Subject: [PATCH 07/15] Let SD can be imported using static library by CocoaPods --- README.md | 11 ++++++++++- SDWebImage.podspec | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e32aa3f..24db8487 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,16 @@ platform :ios, '7.0' pod 'SDWebImage', '~> 4.0' ``` -If you are using Swift, be sure to add `use_frameworks!` and set your target to iOS 8+: +##### Swift + +If you are using `Swift`, `Xcode 9+` and `CocoaPods` `1.5.0+`, you only need to set your target to `iOS 8+` if you need static library: + +``` +platform :ios, '8.0' +``` + +If not, you still need to add `use_frameworks!` to use dynamic framework: + ``` platform :ios, '8.0' use_frameworks! diff --git a/SDWebImage.podspec b/SDWebImage.podspec index 2005f7d4..3b2b49a0 100644 --- a/SDWebImage.podspec +++ b/SDWebImage.podspec @@ -23,6 +23,7 @@ Pod::Spec.new do |s| s.requires_arc = true s.framework = 'ImageIO' + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } s.default_subspec = 'Core' From 2cebcb7419fc175a7c979b84cbd70484ecc25ca0 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 5 Dec 2018 18:36:45 +0800 Subject: [PATCH 08/15] updated CHANGELOG --- CHANGELOG.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e25a09b5..ea6f82fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,9 @@ -## [4.4.3 - 4.4 patch, on Nov 12th, 2018](https://github.com/SDWebImage/SDWebImage/releases/tag/4.4.3) +## [4.4.3 - 4.4 patch, on Nov 25th, 2018](https://github.com/SDWebImage/SDWebImage/releases/tag/4.4.3) See [all tickets marked for the 4.4.3 release](https://github.com/SDWebImage/SDWebImage/milestone/28) #### Fixes - Revert the hack code for `FLAnimatedImage`, because of the `FLAnimatedImage` initializer method blocks the main queue #2441 -- Optimize when scale = 1 #2520 - Fix extention long length of file name #2516 6c6d848 -- Add autoreleasepool to release autorelease objects in advance when using GCD for 4.x #2475 - Fix resource key invalid when clean cached disk file #2463 - Fix the test case `testFLAnimatedImageViewSetImageWithURL` because of remote resource is not available #2450 - Add default `HTTP User-Agent` for specific system #2409 @@ -13,6 +11,14 @@ See [all tickets marked for the 4.4.3 release](https://github.com/SDWebImage/SDW - remove `addProgressCallback`, add `createDownloaderOperationWithUrl` #2336 - Fix the bug when `FLAnimatedImageView` firstly show one EXIF rotation JPEG `UIImage`, later animated GIF `FLAnimatedImage` will also be rotated #2406 - Replace `SDWebImageDownloaderOperation` with `NSOperation` to make generic #2397 +- Fix wrong image cache type when disk and memory cache missed #2529 +- Fix FLAnimatedImage version check issue for custom property `optimalFrameCacheSize` && `predrawingEnabled` #2543 + +#### Performances +- Add autoreleasepool to release autorelease objects in advance when using GCD for 4.x #2475 +- Optimize when scale = 1 #2520 + +#### Docs - Updated URLs after project was transfered to [SDWebImage organization](https://github.com/SDWebImage) #2510 f9d05d9 - Tidy up spacing for `README.md` #2511 - Remove versioneye from README #2424 From cff0f33430eafa17d917b8d6001ae0c0580b53e6 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 9 Dec 2018 11:56:51 +0800 Subject: [PATCH 09/15] Add update image memory cost method --- .../FLAnimatedImage/FLAnimatedImageView+WebCache.m | 8 +------- SDWebImage/SDImageCache.h | 11 +++++++++++ SDWebImage/SDImageCache.m | 12 ++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index dc4dea71..02e56e85 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -175,13 +175,7 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV image.sd_FLAnimatedImage = animatedImage; image.sd_memoryCost = SDWebImageCalculateFLAnimatedImageMemoryCost(animatedImage); NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; - UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:key]; - // If image can be find in memory cache, we update the cost value of cost. - if (cachedImage) { - [[SDImageCache sharedImageCache] removeImageForKey:key fromDisk:NO withCompletion:^{ - [[SDImageCache sharedImageCache] storeImage:image forKey:key toDisk:NO completion:nil]; - }]; - } + [[SDImageCache sharedImageCache] updateImageMemoryCostWithKey:key]; } sstrongSelf.image = animatedImage.posterImage; sstrongSelf.animatedImage = animatedImage; diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index ce259897..6004c3b5 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -160,6 +160,17 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key; +#pragma mark - Update Ops + +/** + * Update image memory cost if image already be stored in memory. + * + * + * @param key The unique image cache key, usually it's image absolute URL + * @note After image be stored in memory cache, if image's memory size changes, such as set a large memory associated object to it, or subclass `UIImage` which has its own large memory properties, you can use this to update memory cost which can make memory cache handle `maxMemoryCost` correctly + */ +- (void)updateImageMemoryCostWithKey:(nullable NSString *)key; + #pragma mark - Query and Retrieve Ops /** diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 062a8bf1..b7c1f57d 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -357,6 +357,18 @@ } } +#pragma mark - Update Ops + +- (void)updateImageMemoryCostWithKey:(NSString *)key { + UIImage *cachedImage = [self imageFromMemoryCacheForKey:key]; + // If image can be find in memory cache, we update the cost value of cost. + if (cachedImage) { + [self removeImageForKey:key fromDisk:NO withCompletion:^{ + [self storeImage:cachedImage forKey:key toDisk:NO completion:nil]; + }]; + } +} + #pragma mark - Query and Retrieve Ops - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock { From 216ba9c7e18ba4d261d39f4df38ff7f33e74f015 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 10 Dec 2018 10:41:17 +0800 Subject: [PATCH 10/15] Fix crash when NSMapTable empty keys --- SDWebImage/SDImageCache.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 412be4d0..b2437c13 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -82,7 +82,9 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { if (key && obj) { // Store weak cache LOCK(self.weakCacheLock); - [self.weakCache setObject:obj forKey:key]; + // Do the real copy of the key and only let NSMapTable manage the key's lifetime + // Fixes issue #2507 https://github.com/SDWebImage/SDWebImage/issues/2507 + [self.weakCache setObject:obj forKey:[[key mutableCopy] copy]]; UNLOCK(self.weakCacheLock); } } From c58642976f9cb955d07398367cc6ac4c3d08984e Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 27 Dec 2018 21:30:07 +0800 Subject: [PATCH 11/15] put FLAnimatedImage fix into another PR --- .../FLAnimatedImage/FLAnimatedImageView+WebCache.m | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index 02e56e85..fc39bafb 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -15,13 +15,6 @@ #import "NSData+ImageContentType.h" #import "UIImageView+WebCache.h" #import "UIImage+MultiFormat.h" -#import "UIImage+CacheMemoryCost.h" - -static inline NSUInteger SDWebImageCalculateFLAnimatedImageMemoryCost(FLAnimatedImage *image) { - UIImage *posterImage = image.posterImage; - // Calculate pixels value, we use pixels as cost to store into memory cache, so we also use image.data's bytes length divide 4. - return image.frameCacheSizeCurrent * posterImage.size.height * posterImage.size.width * posterImage.scale * posterImage.scale + image.data.length / 4; -} static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageView *imageView, NSData *imageData) { if ([NSData sd_imageFormatForImageData:imageData] != SDImageFormatGIF) { @@ -152,7 +145,6 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV dispatch_group_leave(group); return; } - __weak typeof(strongSelf) wweakSelf = strongSelf; // Hack, mark we need should use dispatch group notify for completedBlock objc_setAssociatedObject(group, &SDWebImageInternalSetImageGroupKey, @(YES), OBJC_ASSOCIATION_RETAIN_NONATOMIC); @@ -173,9 +165,6 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV if (animatedImage) { if (sstrongSelf.sd_cacheFLAnimatedImage) { image.sd_FLAnimatedImage = animatedImage; - image.sd_memoryCost = SDWebImageCalculateFLAnimatedImageMemoryCost(animatedImage); - NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; - [[SDImageCache sharedImageCache] updateImageMemoryCostWithKey:key]; } sstrongSelf.image = animatedImage.posterImage; sstrongSelf.animatedImage = animatedImage; From 181d367215aedf6e0ed423215682c4f7ea9af573 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 11 Oct 2018 17:44:18 +0800 Subject: [PATCH 12/15] Fix background download operation FIx background download Add task check when operation will deallocated && tidy code --- SDWebImage/SDWebImageDownloaderOperation.m | 49 +++++++++++----------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index e0a68976..69887e2f 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -64,6 +64,11 @@ typedef NSMutableDictionary SDCallbacksDictionary; @synthesize executing = _executing; @synthesize finished = _finished; +- (void)dealloc { + // Edge case if user call [SDWebImageDownloaderOperation start] directly and deallocated it. + [self cancel]; +} + - (nonnull instancetype)init { return [self initWithRequest:nil inSession:nil options:0]; } @@ -82,6 +87,9 @@ typedef NSMutableDictionary SDCallbacksDictionary; _unownedSession = session; _callbacksLock = dispatch_semaphore_create(1); _coderQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationCoderQueue", DISPATCH_QUEUE_SERIAL); +#if SD_UIKIT + _backgroundTaskId = UIBackgroundTaskInvalid; +#endif } return self; } @@ -135,14 +143,7 @@ typedef NSMutableDictionary SDCallbacksDictionary; __weak __typeof__ (self) wself = self; UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)]; self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{ - __strong __typeof (wself) sself = wself; - - if (sself) { - [sself cancel]; - - [app endBackgroundTask:sself.backgroundTaskId]; - sself.backgroundTaskId = UIBackgroundTaskInvalid; - } + [wself cancel]; }]; } #endif @@ -206,18 +207,6 @@ typedef NSMutableDictionary SDCallbacksDictionary; [self done]; return; } - -#if SD_UIKIT - Class UIApplicationClass = NSClassFromString(@"UIApplication"); - if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) { - return; - } - if (self.backgroundTaskId != UIBackgroundTaskInvalid) { - UIApplication * app = [UIApplication performSelector:@selector(sharedApplication)]; - [app endBackgroundTask:self.backgroundTaskId]; - self.backgroundTaskId = UIBackgroundTaskInvalid; - } -#endif } - (void)cancel { @@ -256,11 +245,23 @@ typedef NSMutableDictionary SDCallbacksDictionary; LOCK(self.callbacksLock); [self.callbackBlocks removeAllObjects]; UNLOCK(self.callbacksLock); - self.dataTask = nil; - if (self.ownedSession) { - [self.ownedSession invalidateAndCancel]; - self.ownedSession = nil; + @synchronized (self) { + self.dataTask = nil; + + if (self.ownedSession) { + [self.ownedSession invalidateAndCancel]; + self.ownedSession = nil; + } + +#if SD_UIKIT + if (self.backgroundTaskId != UIBackgroundTaskInvalid) { + // If backgroundTaskId != UIBackgroundTaskInvalid, sharedApplication is always exist + UIApplication * app = [UIApplication performSelector:@selector(sharedApplication)]; + [app endBackgroundTask:self.backgroundTaskId]; + self.backgroundTaskId = UIBackgroundTaskInvalid; + } +#endif } } From 9725c8da7284ccc87a0e79bd0007a8ce66afae16 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 26 Dec 2018 20:29:09 +0800 Subject: [PATCH 13/15] Remove edge case check --- SDWebImage/SDWebImageDownloaderOperation.m | 5 ----- 1 file changed, 5 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 69887e2f..34d701b0 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -64,11 +64,6 @@ typedef NSMutableDictionary SDCallbacksDictionary; @synthesize executing = _executing; @synthesize finished = _finished; -- (void)dealloc { - // Edge case if user call [SDWebImageDownloaderOperation start] directly and deallocated it. - [self cancel]; -} - - (nonnull instancetype)init { return [self initWithRequest:nil inSession:nil options:0]; } From e6a4e74ed57234701267909151783a5422f355b8 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 3 Jan 2019 11:49:06 +0800 Subject: [PATCH 14/15] Revert the changes for unused public API, polish comments --- SDWebImage/SDImageCache.h | 11 ----------- SDWebImage/SDImageCache.m | 12 ------------ SDWebImage/UIImage+CacheMemoryCost.h | 10 ++++++---- Tests/SDWebImage Tests.xcodeproj/project.pbxproj | 16 ---------------- 4 files changed, 6 insertions(+), 43 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index 6004c3b5..ce259897 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -160,17 +160,6 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key; -#pragma mark - Update Ops - -/** - * Update image memory cost if image already be stored in memory. - * - * - * @param key The unique image cache key, usually it's image absolute URL - * @note After image be stored in memory cache, if image's memory size changes, such as set a large memory associated object to it, or subclass `UIImage` which has its own large memory properties, you can use this to update memory cost which can make memory cache handle `maxMemoryCost` correctly - */ -- (void)updateImageMemoryCostWithKey:(nullable NSString *)key; - #pragma mark - Query and Retrieve Ops /** diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 9f931956..edf2377a 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -359,18 +359,6 @@ } } -#pragma mark - Update Ops - -- (void)updateImageMemoryCostWithKey:(NSString *)key { - UIImage *cachedImage = [self imageFromMemoryCacheForKey:key]; - // If image can be find in memory cache, we update the cost value of cost. - if (cachedImage) { - [self removeImageForKey:key fromDisk:NO withCompletion:^{ - [self storeImage:cachedImage forKey:key toDisk:NO completion:nil]; - }]; - } -} - #pragma mark - Query and Retrieve Ops - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock { diff --git a/SDWebImage/UIImage+CacheMemoryCost.h b/SDWebImage/UIImage+CacheMemoryCost.h index 480230e9..10fa87d9 100644 --- a/SDWebImage/UIImage+CacheMemoryCost.h +++ b/SDWebImage/UIImage+CacheMemoryCost.h @@ -11,10 +11,12 @@ @interface UIImage (CacheMemoryCost) /** - * The image memory cost calculation, this property would be used in memory cache of `SDImageCache`. - * The default value is pixels of `image` or `images`. - * If you set some associated object to `UIImage`, you can set the custom value to indicate the memory cost. - * If you set a new value after `UIImage` be cached to memory cache, you need to reinsert into cache with new value cost by yourself. + The memory cache cost for specify image used by image cache. The cost function is the pixles count held in memory. + If you set some associated object to `UIImage`, you can set the custom value to indicate the memory cost. + + For `UIImage`, this method return the single frame pixles count when `image.images` is nil for static image. Retuen full frame pixels count when `image.images` is not nil for animated image. + For `NSImage`, this method return the single frame pixels count because `NSImage` does not store all frames in memory. + @note Note that because of the limitations of categories this property can get out of sync if you create another instance with CGImage or other methods. */ @property (assign, nonatomic) NSUInteger sd_memoryCost; diff --git a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj index 8013ef4d..5330f6af 100644 --- a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj +++ b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj @@ -159,7 +159,6 @@ DA248D50195472AA00390AB0 /* Frameworks */, DA248D51195472AA00390AB0 /* Resources */, C86216497B5A0BA9501E2C07 /* [CP] Embed Pods Frameworks */, - 85E5D3885A03BFC23B050908 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -232,21 +231,6 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - 85E5D3885A03BFC23B050908 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Tests/Pods-Tests-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; C86216497B5A0BA9501E2C07 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; From c95be0994e3b46ee1a8c8227b83c3f5dad4a5ea2 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 3 Jan 2019 12:08:18 +0800 Subject: [PATCH 15/15] Update the category's naming for memory cache cost, a little code enhancement to avoid NSNumber generation --- SDWebImage.xcodeproj/project.pbxproj | 56 +++++++++---------- SDWebImage/SDImageCache.m | 2 +- ...MemoryCost.h => UIImage+MemoryCacheCost.h} | 2 +- ...MemoryCost.m => UIImage+MemoryCacheCost.m} | 17 +++--- WebImage/SDWebImage.h | 2 +- 5 files changed, 41 insertions(+), 38 deletions(-) rename SDWebImage/{UIImage+CacheMemoryCost.h => UIImage+MemoryCacheCost.h} (96%) rename SDWebImage/{UIImage+CacheMemoryCost.m => UIImage+MemoryCacheCost.m} (63%) diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index e0ddba6b..64f5ce01 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -37,18 +37,18 @@ 00733A711BC4880E00A5A117 /* UIImageView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D95148C56230056699D /* UIImageView+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; 00733A721BC4880E00A5A117 /* UIView+WebCacheOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 00733A731BC4880E00A5A117 /* SDWebImage.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A2CAE031AB4BB5400B6BC39 /* SDWebImage.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0A21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0B21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0C21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0D21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0E21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA0F21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 0E9EFA1021983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; - 0E9EFA1121983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; - 0E9EFA1221983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; - 0E9EFA1321983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; - 0E9EFA1421983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; - 0E9EFA1521983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */; }; + 0E9EFA0A21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0B21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0C21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0D21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0E21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA0F21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0E9EFA1021983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; + 0E9EFA1121983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; + 0E9EFA1221983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; + 0E9EFA1321983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; + 0E9EFA1421983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; + 0E9EFA1521983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */; }; 320224BB203979BA00E9F285 /* SDAnimatedImageRep.h in Headers */ = {isa = PBXBuildFile; fileRef = 320224B9203979BA00E9F285 /* SDAnimatedImageRep.h */; settings = {ATTRIBUTES = (Public, ); }; }; 320224BC203979BA00E9F285 /* SDAnimatedImageRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */; }; 321DB3612011D4D70015D2CB /* NSButton+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 321DB35F2011D4D60015D2CB /* NSButton+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -1301,8 +1301,8 @@ /* Begin PBXFileReference section */ 00733A4C1BC487C000A5A117 /* SDWebImage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImage.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+CacheMemoryCost.h"; sourceTree = ""; }; - 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+CacheMemoryCost.m"; sourceTree = ""; }; + 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+MemoryCacheCost.h"; sourceTree = ""; }; + 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+MemoryCacheCost.m"; sourceTree = ""; }; 320224B9203979BA00E9F285 /* SDAnimatedImageRep.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDAnimatedImageRep.h; sourceTree = ""; }; 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageRep.m; sourceTree = ""; }; 321DB35F2011D4D60015D2CB /* NSButton+WebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSButton+WebCache.h"; path = "SDWebImage/NSButton+WebCache.h"; sourceTree = ""; }; @@ -1804,8 +1804,8 @@ 321E60BD1F38E91700405457 /* UIImage+ForceDecode.m */, AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */, AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */, - 0E9EFA0821983283005D7892 /* UIImage+CacheMemoryCost.h */, - 0E9EFA0921983283005D7892 /* UIImage+CacheMemoryCost.m */, + 0E9EFA0821983283005D7892 /* UIImage+MemoryCacheCost.h */, + 0E9EFA0921983283005D7892 /* UIImage+MemoryCacheCost.m */, ); name = Categories; sourceTree = ""; @@ -2034,7 +2034,7 @@ 4369C27A1D9807EC007E863A /* UIView+WebCache.h in Headers */, 80377DCC1F2F66A700F89830 /* lossless_common.h in Headers */, 321E60971F38E8ED00405457 /* SDWebImageImageIOCoder.h in Headers */, - 0E9EFA0D21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0D21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 43A918671D8308FE00B3925F /* SDImageCacheConfig.h in Headers */, 431739571CDFC8B70008FEB9 /* encode.h in Headers */, 00733A6F1BC4880E00A5A117 /* UIImage+WebP.h in Headers */, @@ -2126,7 +2126,7 @@ 4314D16D1D0E0E3B004B36C9 /* SDImageCache.h in Headers */, 4314D16F1D0E0E3B004B36C9 /* NSData+ImageContentType.h in Headers */, 80377C121F2F666300F89830 /* bit_reader_inl_utils.h in Headers */, - 0E9EFA0B21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0B21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 4314D1701D0E0E3B004B36C9 /* mux.h in Headers */, 321E60871F38E8C800405457 /* SDWebImageCoder.h in Headers */, 80377EA21F2F66D400F89830 /* vp8i_dec.h in Headers */, @@ -2193,7 +2193,7 @@ 431BB6DC1D06D2C1006A3455 /* UIButton+WebCache.h in Headers */, 431BB6E11D06D2C1006A3455 /* SDWebImage.h in Headers */, 80377E311F2F66A800F89830 /* yuv.h in Headers */, - 0E9EFA0E21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0E21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 80377ECA1F2F66D500F89830 /* common_dec.h in Headers */, 80377C771F2F666400F89830 /* thread_utils.h in Headers */, 80377E1F1F2F66A800F89830 /* mips_macro.h in Headers */, @@ -2283,7 +2283,7 @@ 4397D2C81D0DDD8C00BB2784 /* SDWebImageCompat.h in Headers */, 4397D2CB1D0DDD8C00BB2784 /* UIImageView+HighlightedWebCache.h in Headers */, 4397D2CC1D0DDD8C00BB2784 /* mux.h in Headers */, - 0E9EFA0F21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0F21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 80377C911F2F666400F89830 /* thread_utils.h in Headers */, 4397D2D01D0DDD8C00BB2784 /* SDWebImageDownloaderOperation.h in Headers */, 4397D2D11D0DDD8C00BB2784 /* decode.h in Headers */, @@ -2354,7 +2354,7 @@ 80377D871F2F66A700F89830 /* lossless_common.h in Headers */, 321E60961F38E8ED00405457 /* SDWebImageImageIOCoder.h in Headers */, 4A2CAE041AB4BB5400B6BC39 /* SDWebImage.h in Headers */, - 0E9EFA0C21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0C21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 431739511CDFC8B70008FEB9 /* format_constants.h in Headers */, 43A918661D8308FE00B3925F /* SDImageCacheConfig.h in Headers */, 323F8B701F38EF770092B609 /* delta_palettization_enc.h in Headers */, @@ -2450,7 +2450,7 @@ 80377D0B1F2F66A100F89830 /* mips_macro.h in Headers */, 5376131A155AD0D5005750A4 /* SDWebImageDownloader.h in Headers */, 4369C2771D9807EC007E863A /* UIView+WebCache.h in Headers */, - 0E9EFA0A21983283005D7892 /* UIImage+CacheMemoryCost.h in Headers */, + 0E9EFA0A21983283005D7892 /* UIImage+MemoryCacheCost.h in Headers */, 80377CEF1F2F66A100F89830 /* dsp.h in Headers */, 80377C011F2F665300F89830 /* filters_utils.h in Headers */, 5376131C155AD0D5005750A4 /* SDWebImageManager.h in Headers */, @@ -2720,7 +2720,7 @@ 323F8BBD1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0D1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377DBD1F2F66A700F89830 /* dec.c in Sources */, - 0E9EFA1321983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1321983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 00733A561BC4880000A5A117 /* SDWebImageDownloaderOperation.m in Sources */, 80377DE71F2F66A700F89830 /* upsampling.c in Sources */, 321E60C71F38E91700405457 /* UIImage+ForceDecode.m in Sources */, @@ -2995,7 +2995,7 @@ 321E60B71F38E90100405457 /* SDWebImageWebPCoder.m in Sources */, 80377D5D1F2F66A700F89830 /* upsampling.c in Sources */, 80377D251F2F66A700F89830 /* argb.c in Sources */, - 0E9EFA1121983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1121983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 80377D281F2F66A700F89830 /* cost_mips32.c in Sources */, 323F8BF11F38EF770092B609 /* anim_encode.c in Sources */, 4314D1551D0E0E3B004B36C9 /* UIImageView+HighlightedWebCache.m in Sources */, @@ -3140,7 +3140,7 @@ 321E60BA1F38E90100405457 /* SDWebImageWebPCoder.m in Sources */, 80377E2C1F2F66A800F89830 /* upsampling.c in Sources */, 80377DF41F2F66A800F89830 /* argb.c in Sources */, - 0E9EFA1421983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1421983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 80377DF71F2F66A800F89830 /* cost_mips32.c in Sources */, 323F8BF41F38EF770092B609 /* anim_encode.c in Sources */, 80377C6E1F2F666400F89830 /* quant_levels_dec_utils.c in Sources */, @@ -3159,7 +3159,7 @@ 80377E511F2F66A800F89830 /* filters_mips_dsp_r2.c in Sources */, 80377E371F2F66A800F89830 /* argb_mips_dsp_r2.c in Sources */, 80377E471F2F66A800F89830 /* dec.c in Sources */, - 0E9EFA1521983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1521983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 80377C921F2F666400F89830 /* utils.c in Sources */, 4397D27E1D0DDD8C00BB2784 /* UIImage+GIF.m in Sources */, 321E60911F38E8C800405457 /* SDWebImageCoder.m in Sources */, @@ -3308,7 +3308,7 @@ 323F8BBC1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0C1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377D781F2F66A700F89830 /* dec.c in Sources */, - 0E9EFA1221983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1221983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 80377DA21F2F66A700F89830 /* upsampling.c in Sources */, 80377C401F2F666300F89830 /* rescaler_utils.c in Sources */, 321E60C61F38E91700405457 /* UIImage+ForceDecode.m in Sources */, @@ -3457,7 +3457,7 @@ 323F8BBA1F38EF770092B609 /* predictor_enc.c in Sources */, 3290FA0A1FA478AF0047D20C /* SDWebImageFrame.m in Sources */, 80377CEE1F2F66A100F89830 /* dec.c in Sources */, - 0E9EFA1021983283005D7892 /* UIImage+CacheMemoryCost.m in Sources */, + 0E9EFA1021983283005D7892 /* UIImage+MemoryCacheCost.m in Sources */, 80377D181F2F66A100F89830 /* upsampling.c in Sources */, 80377C0C1F2F665300F89830 /* rescaler_utils.c in Sources */, 321E60C41F38E91700405457 /* UIImage+ForceDecode.m in Sources */, diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index edf2377a..015f6ac6 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -9,7 +9,7 @@ #import "SDImageCache.h" #import #import "NSImage+WebCache.h" -#import "UIImage+CacheMemoryCost.h" +#import "UIImage+MemoryCacheCost.h" #import "SDWebImageCodersManager.h" #define SD_MAX_FILE_EXTENSION_LENGTH (NAME_MAX - CC_MD5_DIGEST_LENGTH * 2 - 1) diff --git a/SDWebImage/UIImage+CacheMemoryCost.h b/SDWebImage/UIImage+MemoryCacheCost.h similarity index 96% rename from SDWebImage/UIImage+CacheMemoryCost.h rename to SDWebImage/UIImage+MemoryCacheCost.h index 10fa87d9..9ecf5b94 100644 --- a/SDWebImage/UIImage+CacheMemoryCost.h +++ b/SDWebImage/UIImage+MemoryCacheCost.h @@ -8,7 +8,7 @@ #import "SDWebImageCompat.h" -@interface UIImage (CacheMemoryCost) +@interface UIImage (MemoryCacheCost) /** The memory cache cost for specify image used by image cache. The cost function is the pixles count held in memory. diff --git a/SDWebImage/UIImage+CacheMemoryCost.m b/SDWebImage/UIImage+MemoryCacheCost.m similarity index 63% rename from SDWebImage/UIImage+CacheMemoryCost.m rename to SDWebImage/UIImage+MemoryCacheCost.m index be92eb4b..6f1375d7 100644 --- a/SDWebImage/UIImage+CacheMemoryCost.m +++ b/SDWebImage/UIImage+MemoryCacheCost.m @@ -6,10 +6,10 @@ * file that was distributed with this source code. */ -#import "UIImage+CacheMemoryCost.h" +#import "UIImage+MemoryCacheCost.h" #import "objc/runtime.h" -FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { +FOUNDATION_STATIC_INLINE NSUInteger SDMemoryCacheCostForImage(UIImage *image) { #if SD_MAC return image.size.height * image.size.width; #elif SD_UIKIT || SD_WATCH @@ -18,14 +18,17 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { #endif } -@implementation UIImage (CacheMemoryCost) +@implementation UIImage (MemoryCacheCost) - (NSUInteger)sd_memoryCost { - NSNumber *memoryCost = objc_getAssociatedObject(self, _cmd); - if (memoryCost == nil) { - memoryCost = @(SDCacheCostForImage(self)); + NSNumber *value = objc_getAssociatedObject(self, @selector(sd_memoryCost)); + NSUInteger memoryCost; + if (value != nil) { + memoryCost = [value unsignedIntegerValue]; + } else { + memoryCost = SDMemoryCacheCostForImage(self); } - return [memoryCost unsignedIntegerValue]; + return memoryCost; } - (void)setSd_memoryCost:(NSUInteger)sd_memoryCost { diff --git a/WebImage/SDWebImage.h b/WebImage/SDWebImage.h index cdf3109f..d1766cf7 100644 --- a/WebImage/SDWebImage.h +++ b/WebImage/SDWebImage.h @@ -32,7 +32,7 @@ FOUNDATION_EXPORT const unsigned char WebImageVersionString[]; #import #import #import -#import +#import #import #import #import