From c7f2665d05b93d2162ee35bc17f9fa2fccd2b80b Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 22 Nov 2013 16:16:03 -0800 Subject: [PATCH 1/2] Allow setting a custom memory cost limit for the in-memory NSCache. This can help to keep memory down before memory warnings are sent by the device. --- SDWebImage/SDImageCache.h | 5 +++++ SDWebImage/SDImageCache.m | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index 4a571c1c..c6b63f9b 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -32,6 +32,11 @@ typedef enum SDImageCacheType SDImageCacheType; */ @interface SDImageCache : NSObject +/** + * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory. + */ +@property (assign, nonatomic) NSInteger maxMemoryCost; + /** * The maximum length of time to keep an image in the cache, in seconds */ diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index e55ec71a..c8748c3f 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -338,6 +338,11 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week } } +- (void)setMaxMemoryCost:(NSInteger)maxMemoryCost +{ + self.memCache.totalCostLimit = maxMemoryCost; +} + - (void)clearMemory { [self.memCache removeAllObjects]; From c3ee5896816ad92580e831c624786d912a4500c5 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 22 Nov 2013 16:38:18 -0800 Subject: [PATCH 2/2] NSInteger -> NSUInteger, add getter method --- SDWebImage/SDImageCache.h | 2 +- SDWebImage/SDImageCache.m | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index c6b63f9b..7a76ca19 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -35,7 +35,7 @@ typedef enum SDImageCacheType SDImageCacheType; /** * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory. */ -@property (assign, nonatomic) NSInteger maxMemoryCost; +@property (assign, nonatomic) NSUInteger maxMemoryCost; /** * The maximum length of time to keep an image in the cache, in seconds diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index c8748c3f..8ffed7b1 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -338,11 +338,16 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week } } -- (void)setMaxMemoryCost:(NSInteger)maxMemoryCost +- (void)setMaxMemoryCost:(NSUInteger)maxMemoryCost { self.memCache.totalCostLimit = maxMemoryCost; } +- (NSUInteger)maxMemoryCost +{ + return self.memCache.totalCostLimit; +} + - (void)clearMemory { [self.memCache removeAllObjects];