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);