From 55ebe66b4c3b4f2fad0d7f597832b620cfbfad18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20Mu=CC=88ller?= Date: Tue, 23 Apr 2013 09:26:35 +0200 Subject: [PATCH 1/2] Fix and make SDScaledImageForPath extensible --- SDWebImage/SDImageCache.m | 27 ++++++++++++++++++---- SDWebImage/SDWebImageCompat.h | 2 +- SDWebImage/SDWebImageCompat.m | 26 +++------------------ SDWebImage/SDWebImageDownloaderOperation.m | 13 +++++++++-- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 45cd7563..00e776ed 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -164,8 +164,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week } // Second check the disk cache... - UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])]; - + UIImage *diskImage = [self diskImageForKey:key]; if (diskImage) { CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; @@ -175,6 +174,27 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week return diskImage; } +- (UIImage *)diskImageForKey:(NSString *)key +{ + NSString *path = [self cachePathForKey:key]; + NSData *data = [NSData dataWithContentsOfFile:path]; + if (data) + { + UIImage *image = [[UIImage alloc] initWithData:data]; + UIImage *scaledImage = [self scaledImageForKey:key image:image]; + return [UIImage decodedImageWithImage:scaledImage]; + } + else + { + return nil; + } +} + +- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image +{ + return SDScaledImageForKey(key, image); +} + - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock { if (!doneBlock) return; @@ -197,8 +217,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week { @autoreleasepool { - UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])]; - + UIImage *diskImage = [self diskImageForKey:key]; if (diskImage) { CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale; diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index a0750456..b14b8cb8 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -37,4 +37,4 @@ #define SDDispatchQueueSetterSementics assign #endif -extern inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData); +extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); \ No newline at end of file diff --git a/SDWebImage/SDWebImageCompat.m b/SDWebImage/SDWebImageCompat.m index 1ff3afbe..6d54e51a 100644 --- a/SDWebImage/SDWebImageCompat.m +++ b/SDWebImage/SDWebImageCompat.m @@ -12,34 +12,15 @@ #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag #endif -inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) +inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) { - if (!imageOrData) - { - return nil; - } - - UIImage *image = nil; - if ([imageOrData isKindOfClass:[NSData class]]) - { - image = [[UIImage alloc] initWithData:(NSData *)imageOrData]; - } - else if ([imageOrData isKindOfClass:[UIImage class]]) - { - image = (UIImage *)imageOrData; - } - else - { - return nil; - } - if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { CGFloat scale = 1.0; - if (path.length >= 8) + if (key.length >= 8) { // 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) - NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)]; + NSRange range = [key rangeOfString:@"@2x." options:0 range:NSMakeRange(key.length - 8, 5)]; if (range.location != NSNotFound) { scale = 2.0; @@ -49,6 +30,5 @@ inline UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; image = scaledImage; } - return image; } diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index f8e72209..a194966a 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -225,7 +225,9 @@ if (partialImageRef) { - UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, [UIImage imageWithCGImage:partialImageRef])]; + UIImage *image = [UIImage imageWithCGImage:partialImageRef]; + UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; + image = [UIImage decodedImageWithImage:scaledImage]; CGImageRelease(partialImageRef); dispatch_async(dispatch_get_main_queue(), ^ { @@ -248,6 +250,11 @@ } } +- (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image +{ + return SDScaledImageForKey(key, image); +} + - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection { CFRunLoopStop(CFRunLoopGetCurrent()); @@ -267,7 +274,9 @@ } else { - UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)]; + UIImage *image = [[UIImage alloc] initWithData:self.imageData]; + UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; + image = [UIImage decodedImageWithImage:scaledImage]; if (CGSizeEqualToSize(image.size, CGSizeZero)) { completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Downloaded image has 0 pixels"}], YES); From 9eff469cb65be5814cd5043303228999f738c9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 23 Apr 2013 09:36:07 +0200 Subject: [PATCH 2/2] Make SDWebImageManager cache creation extensible --- SDWebImage/SDWebImageManager.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index c7fee6a2..8f251261 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -39,7 +39,7 @@ { if ((self = [super init])) { - _imageCache = [SDImageCache sharedImageCache]; + _imageCache = [self createCache]; _imageDownloader = SDWebImageDownloader.new; _failedURLs = NSMutableArray.new; _runningOperations = NSMutableArray.new; @@ -47,6 +47,10 @@ return self; } +- (SDImageCache *)createCache +{ + return [SDImageCache sharedImageCache]; +} - (NSString *)cacheKeyForURL:(NSURL *)url {