Authored by Olivier Poitrey

Refactor 2x scale support

@@ -169,28 +169,14 @@ static SDImageCache *instance; @@ -169,28 +169,14 @@ static SDImageCache *instance;
169 } 169 }
170 } 170 }
171 } 171 }
172 -- (UIImage *) imageForFile:(NSString*)fileKey {  
173 - NSString *file = [self cachePathForKey:fileKey];  
174 - NSData *imageData = [NSData dataWithContentsOfFile:file];  
175 - if (imageData) {  
176 - UIImage *image = [[[UIImage alloc] initWithData:imageData ] autorelease];  
177 - if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {  
178 - CGFloat scale = 1.0;  
179 - if ([fileKey hasSuffix:@"@2x.png"] || [fileKey hasSuffix:@"@2x.jpg"]) {  
180 - scale = 2.0;  
181 - }  
182 - image = [[[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp] autorelease];  
183 - }  
184 - return image;  
185 - }  
186 - return nil;  
187 -} 172 +
188 - (void)queryDiskCacheOperation:(NSDictionary *)arguments 173 - (void)queryDiskCacheOperation:(NSDictionary *)arguments
189 { 174 {
190 NSString *key = [arguments objectForKey:@"key"]; 175 NSString *key = [arguments objectForKey:@"key"];
191 NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease]; 176 NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease];
192 -  
193 - UIImage *image = [self imageForFile:key]; 177 +
  178 + UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
  179 +
194 if (image) 180 if (image)
195 { 181 {
196 #ifdef ENABLE_SDWEBIMAGE_DECODER 182 #ifdef ENABLE_SDWEBIMAGE_DECODER
@@ -262,7 +248,7 @@ static SDImageCache *instance; @@ -262,7 +248,7 @@ static SDImageCache *instance;
262 248
263 if (!image && fromDisk) 249 if (!image && fromDisk)
264 { 250 {
265 - UIImage *image = [self imageForFile:key]; 251 + UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
266 if (image) 252 if (image)
267 { 253 {
268 [memCache setObject:image forKey:key]; 254 [memCache setObject:image forKey:key];
@@ -200,7 +200,7 @@ @@ -200,7 +200,7 @@
200 53922D66148C55810056699D /* Project object */ = { 200 53922D66148C55810056699D /* Project object */ = {
201 isa = PBXProject; 201 isa = PBXProject;
202 attributes = { 202 attributes = {
203 - LastUpgradeCheck = 0420; 203 + LastUpgradeCheck = 0430;
204 ORGANIZATIONNAME = Dailymotion; 204 ORGANIZATIONNAME = Dailymotion;
205 }; 205 };
206 buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */; 206 buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */;
1 -//  
2 -// SDWebImageCompat.h  
3 -// SDWebImageCompat  
4 -//  
5 -// Created by Jamie Pinkham on 3/15/11.  
6 -// Copyright 2011 __MyCompanyName__. All rights reserved.  
7 -// 1 +/*
  2 + * This file is part of the SDWebImage package.
  3 + * (c) Olivier Poitrey <rs@dailymotion.com>
  4 + * (c) Jamie Pinkham
  5 + *
  6 + * For the full copyright and license information, please view the LICENSE
  7 + * file that was distributed with this source code.
  8 + */
8 9
9 #import <TargetConditionals.h> 10 #import <TargetConditionals.h>
10 11
@@ -19,3 +20,33 @@ @@ -19,3 +20,33 @@
19 #else 20 #else
20 #import <UIKit/UIKit.h> 21 #import <UIKit/UIKit.h>
21 #endif 22 #endif
  23 +
  24 +NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSData *imageData)
  25 +{
  26 + if (!imageData)
  27 + {
  28 + return nil;
  29 + }
  30 +
  31 + UIImage *image = [[UIImage alloc] initWithData:imageData];
  32 +
  33 + if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
  34 + {
  35 + CGFloat scale = 1.0;
  36 + if (path.length >= 8)
  37 + {
  38 + // 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)
  39 + NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)];
  40 + if (range.location != NSNotFound)
  41 + {
  42 + scale = 2.0;
  43 + }
  44 + }
  45 +
  46 + UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
  47 + [image release];
  48 + image = scaledImage;
  49 + }
  50 +
  51 + return [image autorelease];
  52 +}
@@ -125,18 +125,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot @@ -125,18 +125,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
125 125
126 if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)]) 126 if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
127 { 127 {
128 - CGFloat scale = 1.0;  
129 - NSString *lastPathComponent = url.absoluteString;  
130 - if ([lastPathComponent hasSuffix:@"@2x.png"] || [lastPathComponent hasSuffix:@"@2x.jpg"]) {  
131 - scale = 2.0;  
132 - }  
133 -  
134 - UIImage *image = [[UIImage alloc] initWithData:imageData ];  
135 - if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {  
136 - UIImage *originalImage = image;  
137 - image = [[UIImage alloc] initWithCGImage:originalImage.CGImage scale:scale orientation:UIImageOrientationUp];  
138 - [originalImage release];  
139 - } 128 + UIImage *image = SDScaledImageForPath(url.absoluteString, imageData);
140 129
141 #ifdef ENABLE_SDWEBIMAGE_DECODER 130 #ifdef ENABLE_SDWEBIMAGE_DECODER
142 [[SDWebImageDecoder sharedImageDecoder] decodeImage:image withDelegate:self userInfo:nil]; 131 [[SDWebImageDecoder sharedImageDecoder] decodeImage:image withDelegate:self userInfo:nil];