瀏覽代碼

Refactor 2x scale support

Olivier Poitrey 14 年之前
父節點
當前提交
aa6956e9fc
共有 4 個文件被更改,包括 45 次插入39 次删除
  1. 5 19
      SDImageCache.m
  2. 1 1
      SDWebImage.xcodeproj/project.pbxproj
  3. 38 7
      SDWebImageCompat.h
  4. 1 12
      SDWebImageDownloader.m

+ 5 - 19
SDImageCache.m

@@ -169,28 +169,14 @@ static SDImageCache *instance;
         }
     }
 }
-- (UIImage *) imageForFile:(NSString*)fileKey {
-    NSString *file = [self cachePathForKey:fileKey];
-    NSData *imageData = [NSData dataWithContentsOfFile:file];
-    if (imageData) {
-        UIImage *image = [[[UIImage alloc] initWithData:imageData ] autorelease];
-        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
-            CGFloat scale = 1.0;
-            if ([fileKey hasSuffix:@"@2x.png"] || [fileKey hasSuffix:@"@2x.jpg"]) {
-                scale = 2.0;
-            }
-            image = [[[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp] autorelease];
-        }
-        return image;
-    }
-    return nil;
-}
+
 - (void)queryDiskCacheOperation:(NSDictionary *)arguments
 {
     NSString *key = [arguments objectForKey:@"key"];
     NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease];
-    
-    UIImage *image = [self imageForFile:key];
+
+    UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
+
     if (image)
     {
 #ifdef ENABLE_SDWEBIMAGE_DECODER
@@ -262,7 +248,7 @@ static SDImageCache *instance;
 
     if (!image && fromDisk)
     {
-        UIImage *image = [self imageForFile:key];
+        UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
         if (image)
         {
             [memCache setObject:image forKey:key];

+ 1 - 1
SDWebImage.xcodeproj/project.pbxproj

@@ -200,7 +200,7 @@
 		53922D66148C55810056699D /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
-				LastUpgradeCheck = 0420;
+				LastUpgradeCheck = 0430;
 				ORGANIZATIONNAME = Dailymotion;
 			};
 			buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */;

+ 38 - 7
SDWebImageCompat.h

@@ -1,10 +1,11 @@
-//
-//  SDWebImageCompat.h
-//  SDWebImageCompat
-//
-//  Created by Jamie Pinkham on 3/15/11.
-//  Copyright 2011 __MyCompanyName__. All rights reserved.
-//
+/*
+ * This file is part of the SDWebImage package.
+ * (c) Olivier Poitrey <rs@dailymotion.com>
+ * (c) Jamie Pinkham
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
 
 #import <TargetConditionals.h>
 
@@ -19,3 +20,33 @@
 #else
 #import <UIKit/UIKit.h>
 #endif
+
+NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSData *imageData)
+{
+    if (!imageData)
+    {
+        return nil;
+    }
+
+    UIImage *image = [[UIImage alloc] initWithData:imageData];
+
+    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
+    {
+        CGFloat scale = 1.0;
+        if (path.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)];
+            if (range.location != NSNotFound)
+            {
+                scale = 2.0;
+            }
+        }
+
+        UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
+        [image release];
+        image = scaledImage;
+    }
+
+    return [image autorelease];
+}

+ 1 - 12
SDWebImageDownloader.m

@@ -125,18 +125,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
 
     if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
     {
-        CGFloat scale = 1.0;
-        NSString *lastPathComponent = url.absoluteString;
-        if ([lastPathComponent hasSuffix:@"@2x.png"] || [lastPathComponent hasSuffix:@"@2x.jpg"]) {
-            scale = 2.0;
-        }
-        
-        UIImage *image = [[UIImage alloc] initWithData:imageData ];
-        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
-            UIImage *originalImage = image;
-            image = [[UIImage alloc] initWithCGImage:originalImage.CGImage scale:scale orientation:UIImageOrientationUp];
-            [originalImage release];
-        }
+        UIImage *image = SDScaledImageForPath(url.absoluteString, imageData);
 
 #ifdef ENABLE_SDWEBIMAGE_DECODER
         [[SDWebImageDecoder sharedImageDecoder] decodeImage:image withDelegate:self userInfo:nil];