Refactor 2x scale support
This commit is contained in:
parent
c972489931
commit
aa6956e9fc
|
|
@ -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
|
- (void)queryDiskCacheOperation:(NSDictionary *)arguments
|
||||||
{
|
{
|
||||||
NSString *key = [arguments objectForKey:@"key"];
|
NSString *key = [arguments objectForKey:@"key"];
|
||||||
NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease];
|
NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease];
|
||||||
|
|
||||||
UIImage *image = [self imageForFile:key];
|
UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
|
||||||
|
|
||||||
if (image)
|
if (image)
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_SDWEBIMAGE_DECODER
|
#ifdef ENABLE_SDWEBIMAGE_DECODER
|
||||||
|
|
@ -262,7 +248,7 @@ static SDImageCache *instance;
|
||||||
|
|
||||||
if (!image && fromDisk)
|
if (!image && fromDisk)
|
||||||
{
|
{
|
||||||
UIImage *image = [self imageForFile:key];
|
UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]);
|
||||||
if (image)
|
if (image)
|
||||||
{
|
{
|
||||||
[memCache setObject:image forKey:key];
|
[memCache setObject:image forKey:key];
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,7 @@
|
||||||
53922D66148C55810056699D /* Project object */ = {
|
53922D66148C55810056699D /* Project object */ = {
|
||||||
isa = PBXProject;
|
isa = PBXProject;
|
||||||
attributes = {
|
attributes = {
|
||||||
LastUpgradeCheck = 0420;
|
LastUpgradeCheck = 0430;
|
||||||
ORGANIZATIONNAME = Dailymotion;
|
ORGANIZATIONNAME = Dailymotion;
|
||||||
};
|
};
|
||||||
buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */;
|
buildConfigurationList = 53922D69148C55810056699D /* Build configuration list for PBXProject "SDWebImage" */;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
//
|
/*
|
||||||
// SDWebImageCompat.h
|
* This file is part of the SDWebImage package.
|
||||||
// SDWebImageCompat
|
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||||
//
|
* (c) Jamie Pinkham
|
||||||
// Created by Jamie Pinkham on 3/15/11.
|
*
|
||||||
// Copyright 2011 __MyCompanyName__. All rights reserved.
|
* For the full copyright and license information, please view the LICENSE
|
||||||
//
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
#import <TargetConditionals.h>
|
#import <TargetConditionals.h>
|
||||||
|
|
||||||
|
|
@ -19,3 +20,33 @@
|
||||||
#else
|
#else
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
#endif
|
#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];
|
||||||
|
}
|
||||||
|
|
@ -125,18 +125,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot
|
||||||
|
|
||||||
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
||||||
{
|
{
|
||||||
CGFloat scale = 1.0;
|
UIImage *image = SDScaledImageForPath(url.absoluteString, imageData);
|
||||||
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];
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef ENABLE_SDWEBIMAGE_DECODER
|
#ifdef ENABLE_SDWEBIMAGE_DECODER
|
||||||
[[SDWebImageDecoder sharedImageDecoder] decodeImage:image withDelegate:self userInfo:nil];
|
[[SDWebImageDecoder sharedImageDecoder] decodeImage:image withDelegate:self userInfo:nil];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue