Permalink
Browse files

Refactor 2x scale support

  • Loading branch information...
rs committed Mar 10, 2012
1 parent c972489 commit aa6956e9fc1d53aca1eda16ba5139da3cfebfed4
Showing with 45 additions and 39 deletions.
  1. +5 −19 SDImageCache.m
  2. +1 −1 SDWebImage.xcodeproj/project.pbxproj
  3. +38 −7 SDWebImageCompat.h
  4. +1 −12 SDWebImageDownloader.m
View
@@ -169,28 +169,14 @@ - (void)notifyDelegate:(NSDictionary *)arguments
}
}
}
-- (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 @@ - (UIImage *)imageFromKey:(NSString *)key fromDisk:(BOOL)fromDisk
if (!image && fromDisk)
{
- UIImage *image = [self imageForFile:key];
+ UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
if (image)
{
[memCache setObject:image forKey:key];
@@ -200,7 +200,7 @@
53922D66148C55810056699D /* Project object */ = {
isa = PBXProject;
attributes = {
- LastUpgradeCheck = 0420;
+ LastUpgradeCheck = 0430;
ORGANIZATIONNAME = Dailymotion;
};
buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */;
View
@@ -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];
+}
View
@@ -125,18 +125,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
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];

0 comments on commit aa6956e

Please sign in to comment.