Refactor 2x scale support

This commit is contained in:
Olivier Poitrey 2012-03-10 16:22:14 +01:00
parent c972489931
commit aa6956e9fc
4 changed files with 45 additions and 39 deletions

View File

@ -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];

View File

@ -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 File

@ -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 File

@ -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];