From aa6956e9fc1d53aca1eda16ba5139da3cfebfed4 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Sat, 10 Mar 2012 16:22:14 +0100 Subject: [PATCH] Refactor 2x scale support --- SDImageCache.m | 24 ++++----------- SDWebImage.xcodeproj/project.pbxproj | 2 +- SDWebImageCompat.h | 45 +++++++++++++++++++++++----- SDWebImageDownloader.m | 13 +------- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/SDImageCache.m b/SDImageCache.m index 3bf426d..6a110ac 100644 --- a/SDImageCache.m +++ b/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]; diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index 9fa0b86..2debffa 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/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" */; diff --git a/SDWebImageCompat.h b/SDWebImageCompat.h index 24effc4..5c86b6b 100644 --- a/SDWebImageCompat.h +++ b/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 + * (c) Jamie Pinkham + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ #import @@ -19,3 +20,33 @@ #else #import #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]; +} \ No newline at end of file diff --git a/SDWebImageDownloader.m b/SDWebImageDownloader.m index bd1778a..12cacd1 100644 --- a/SDWebImageDownloader.m +++ b/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];