Skip to content

Commit

Permalink
Merge pull request SDWebImage#1075 from ikesyo/refactor-image-cache-cost
Browse files Browse the repository at this point in the history
[Refactor] Implement cache cost calculation as a inline function
  • Loading branch information
bpoplauschi committed Mar 19, 2015
2 parents d56c4f3 + 021607a commit 2b68869
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,7 +161,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, ^{
Expand Down Expand Up @@ -244,7 +249,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];
}

Expand Down Expand Up @@ -315,7 +320,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];
}

Expand Down

0 comments on commit 2b68869

Please sign in to comment.