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

[Refactor] Implement cache cost calculation as a inline function
This commit is contained in:
Bogdan Poplauschi 2015-03-19 14:23:33 +02:00
commit 2b68869927
1 changed files with 8 additions and 3 deletions

View File

@ -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;
@ -157,7 +161,8 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
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, ^{
@ -244,7 +249,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
// 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];
}
@ -315,7 +320,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
@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];
}