From 021607a84543012847af6eef9d5add3ce295567a Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Wed, 18 Mar 2015 12:27:23 +0900 Subject: [PATCH] [Refactor] Implement cache cost calculation as a inline function --- SDWebImage/SDImageCache.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 00529037e..e0d71dea3 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -29,6 +29,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { return NO; } +FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { + return image.size.height * image.size.width * image.scale * image.scale; +} + @interface SDImageCache () @property (strong, nonatomic) NSCache *memCache; @@ -152,7 +156,8 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image return; } - [self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale * image.scale]; + NSUInteger cost = SDCacheCostForImage(image); + [self.memCache setObject:image forKey:key cost:cost]; if (toDisk) { dispatch_async(self.ioQueue, ^{ @@ -239,7 +244,7 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key { // Second check the disk cache... UIImage *diskImage = [self diskImageForKey:key]; if (diskImage) { - CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale; + NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost]; } @@ -309,7 +314,7 @@ - (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompl @autoreleasepool { UIImage *diskImage = [self diskImageForKey:key]; if (diskImage) { - CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale; + NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost]; }