Authored by Bogdan Poplauschi

Merge pull request #1075 from ikesyo/refactor-image-cache-cost

[Refactor] Implement cache cost calculation as a inline function
@@ -29,6 +29,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -29,6 +29,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
29 return NO; 29 return NO;
30 } 30 }
31 31
  32 +FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
  33 + return image.size.height * image.size.width * image.scale * image.scale;
  34 +}
  35 +
32 @interface SDImageCache () 36 @interface SDImageCache ()
33 37
34 @property (strong, nonatomic) NSCache *memCache; 38 @property (strong, nonatomic) NSCache *memCache;
@@ -157,7 +161,8 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -157,7 +161,8 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
157 return; 161 return;
158 } 162 }
159 163
160 - [self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale * image.scale]; 164 + NSUInteger cost = SDCacheCostForImage(image);
  165 + [self.memCache setObject:image forKey:key cost:cost];
161 166
162 if (toDisk) { 167 if (toDisk) {
163 dispatch_async(self.ioQueue, ^{ 168 dispatch_async(self.ioQueue, ^{
@@ -244,7 +249,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -244,7 +249,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
244 // Second check the disk cache... 249 // Second check the disk cache...
245 UIImage *diskImage = [self diskImageForKey:key]; 250 UIImage *diskImage = [self diskImageForKey:key];
246 if (diskImage) { 251 if (diskImage) {
247 - CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale; 252 + NSUInteger cost = SDCacheCostForImage(diskImage);
248 [self.memCache setObject:diskImage forKey:key cost:cost]; 253 [self.memCache setObject:diskImage forKey:key cost:cost];
249 } 254 }
250 255
@@ -315,7 +320,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -315,7 +320,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
315 @autoreleasepool { 320 @autoreleasepool {
316 UIImage *diskImage = [self diskImageForKey:key]; 321 UIImage *diskImage = [self diskImageForKey:key];
317 if (diskImage) { 322 if (diskImage) {
318 - CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale * diskImage.scale; 323 + NSUInteger cost = SDCacheCostForImage(diskImage);
319 [self.memCache setObject:diskImage forKey:key cost:cost]; 324 [self.memCache setObject:diskImage forKey:key cost:cost];
320 } 325 }
321 326