Merge pull request #366 from bm-i/master
Fix and make SDScaledImageForPath extensible
Showing
5 changed files
with
43 additions
and
31 deletions
@@ -164,8 +164,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | @@ -164,8 +164,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | ||
164 | } | 164 | } |
165 | 165 | ||
166 | // Second check the disk cache... | 166 | // Second check the disk cache... |
167 | - UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])]; | ||
168 | - | 167 | + UIImage *diskImage = [self diskImageForKey:key]; |
169 | if (diskImage) | 168 | if (diskImage) |
170 | { | 169 | { |
171 | CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; | 170 | CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; |
@@ -175,6 +174,27 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | @@ -175,6 +174,27 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | ||
175 | return diskImage; | 174 | return diskImage; |
176 | } | 175 | } |
177 | 176 | ||
177 | +- (UIImage *)diskImageForKey:(NSString *)key | ||
178 | +{ | ||
179 | + NSString *path = [self cachePathForKey:key]; | ||
180 | + NSData *data = [NSData dataWithContentsOfFile:path]; | ||
181 | + if (data) | ||
182 | + { | ||
183 | + UIImage *image = [[UIImage alloc] initWithData:data]; | ||
184 | + UIImage *scaledImage = [self scaledImageForKey:key image:image]; | ||
185 | + return [UIImage decodedImageWithImage:scaledImage]; | ||
186 | + } | ||
187 | + else | ||
188 | + { | ||
189 | + return nil; | ||
190 | + } | ||
191 | +} | ||
192 | + | ||
193 | +- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image | ||
194 | +{ | ||
195 | + return SDScaledImageForKey(key, image); | ||
196 | +} | ||
197 | + | ||
178 | - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock | 198 | - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock |
179 | { | 199 | { |
180 | if (!doneBlock) return; | 200 | if (!doneBlock) return; |
@@ -197,8 +217,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | @@ -197,8 +217,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week | ||
197 | { | 217 | { |
198 | @autoreleasepool | 218 | @autoreleasepool |
199 | { | 219 | { |
200 | - UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])]; | ||
201 | - | 220 | + UIImage *diskImage = [self diskImageForKey:key]; |
202 | if (diskImage) | 221 | if (diskImage) |
203 | { | 222 | { |
204 | CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; | 223 | CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; |
@@ -37,4 +37,4 @@ | @@ -37,4 +37,4 @@ | ||
37 | #define SDDispatchQueueSetterSementics assign | 37 | #define SDDispatchQueueSetterSementics assign |
38 | #endif | 38 | #endif |
39 | 39 | ||
40 | -extern inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData); | 40 | +extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); |
@@ -12,34 +12,15 @@ | @@ -12,34 +12,15 @@ | ||
12 | #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag | 12 | #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag |
13 | #endif | 13 | #endif |
14 | 14 | ||
15 | -inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) | 15 | +inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) |
16 | { | 16 | { |
17 | - if (!imageOrData) | ||
18 | - { | ||
19 | - return nil; | ||
20 | - } | ||
21 | - | ||
22 | - UIImage *image = nil; | ||
23 | - if ([imageOrData isKindOfClass:[NSData class]]) | ||
24 | - { | ||
25 | - image = [[UIImage alloc] initWithData:(NSData *)imageOrData]; | ||
26 | - } | ||
27 | - else if ([imageOrData isKindOfClass:[UIImage class]]) | ||
28 | - { | ||
29 | - image = (UIImage *)imageOrData; | ||
30 | - } | ||
31 | - else | ||
32 | - { | ||
33 | - return nil; | ||
34 | - } | ||
35 | - | ||
36 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) | 17 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) |
37 | { | 18 | { |
38 | CGFloat scale = 1.0; | 19 | CGFloat scale = 1.0; |
39 | - if (path.length >= 8) | 20 | + if (key.length >= 8) |
40 | { | 21 | { |
41 | // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext) | 22 | // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext) |
42 | - NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)]; | 23 | + NSRange range = [key rangeOfString:@"@2x." options:0 range:NSMakeRange(key.length - 8, 5)]; |
43 | if (range.location != NSNotFound) | 24 | if (range.location != NSNotFound) |
44 | { | 25 | { |
45 | scale = 2.0; | 26 | scale = 2.0; |
@@ -49,6 +30,5 @@ inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) | @@ -49,6 +30,5 @@ inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) | ||
49 | UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; | 30 | UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; |
50 | image = scaledImage; | 31 | image = scaledImage; |
51 | } | 32 | } |
52 | - | ||
53 | return image; | 33 | return image; |
54 | } | 34 | } |
@@ -225,7 +225,9 @@ | @@ -225,7 +225,9 @@ | ||
225 | 225 | ||
226 | if (partialImageRef) | 226 | if (partialImageRef) |
227 | { | 227 | { |
228 | - UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, [UIImage imageWithCGImage:partialImageRef])]; | 228 | + UIImage *image = [UIImage imageWithCGImage:partialImageRef]; |
229 | + UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; | ||
230 | + image = [UIImage decodedImageWithImage:scaledImage]; | ||
229 | CGImageRelease(partialImageRef); | 231 | CGImageRelease(partialImageRef); |
230 | dispatch_async(dispatch_get_main_queue(), ^ | 232 | dispatch_async(dispatch_get_main_queue(), ^ |
231 | { | 233 | { |
@@ -248,6 +250,11 @@ | @@ -248,6 +250,11 @@ | ||
248 | } | 250 | } |
249 | } | 251 | } |
250 | 252 | ||
253 | +- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image | ||
254 | +{ | ||
255 | + return SDScaledImageForKey(key, image); | ||
256 | +} | ||
257 | + | ||
251 | - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection | 258 | - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection |
252 | { | 259 | { |
253 | CFRunLoopStop(CFRunLoopGetCurrent()); | 260 | CFRunLoopStop(CFRunLoopGetCurrent()); |
@@ -267,7 +274,9 @@ | @@ -267,7 +274,9 @@ | ||
267 | } | 274 | } |
268 | else | 275 | else |
269 | { | 276 | { |
270 | - UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)]; | 277 | + UIImage *image = [[UIImage alloc] initWithData:self.imageData]; |
278 | + UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; | ||
279 | + image = [UIImage decodedImageWithImage:scaledImage]; | ||
271 | if (CGSizeEqualToSize(image.size, CGSizeZero)) | 280 | if (CGSizeEqualToSize(image.size, CGSizeZero)) |
272 | { | 281 | { |
273 | completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES); | 282 | completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES); |
@@ -39,7 +39,7 @@ | @@ -39,7 +39,7 @@ | ||
39 | { | 39 | { |
40 | if ((self = [super init])) | 40 | if ((self = [super init])) |
41 | { | 41 | { |
42 | - _imageCache = [SDImageCache sharedImageCache]; | 42 | + _imageCache = [self createCache]; |
43 | _imageDownloader = SDWebImageDownloader.new; | 43 | _imageDownloader = SDWebImageDownloader.new; |
44 | _failedURLs = NSMutableArray.new; | 44 | _failedURLs = NSMutableArray.new; |
45 | _runningOperations = NSMutableArray.new; | 45 | _runningOperations = NSMutableArray.new; |
@@ -47,6 +47,10 @@ | @@ -47,6 +47,10 @@ | ||
47 | return self; | 47 | return self; |
48 | } | 48 | } |
49 | 49 | ||
50 | +- (SDImageCache *)createCache | ||
51 | +{ | ||
52 | + return [SDImageCache sharedImageCache]; | ||
53 | +} | ||
50 | 54 | ||
51 | - (NSString *)cacheKeyForURL:(NSURL *)url | 55 | - (NSString *)cacheKeyForURL:(NSURL *)url |
52 | { | 56 | { |
-
Please register or login to post a comment