From 4c5bdbd7de5b849972503b45f9a1541056f276f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Petr=C5=AF?= Date: Sat, 10 Dec 2011 19:00:49 +0100 Subject: [PATCH] Added scaling while constructing image from cached data. --- AFNetworking/AFImageCache.h | 7 +++++++ AFNetworking/AFImageCache.m | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFImageCache.h b/AFNetworking/AFImageCache.h index 159c9c5..d2f15a9 100644 --- a/AFNetworking/AFImageCache.h +++ b/AFNetworking/AFImageCache.h @@ -32,6 +32,13 @@ */ @interface AFImageCache : NSCache +#if __IPHONE_OS_VERSION_MIN_REQUIRED +/** + The scale factor used when interpreting the cached image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property. This is set to the value of `[[UIScreen mainScreen] scale]` by default, which automatically scales images for retina displays, for instance. + */ +@property (nonatomic, assign) CGFloat imageScale; +#endif + /** Returns the shared image cache object for the system. diff --git a/AFNetworking/AFImageCache.m b/AFNetworking/AFImageCache.m index 88f04ef..86a4e69 100644 --- a/AFNetworking/AFImageCache.m +++ b/AFNetworking/AFImageCache.m @@ -28,6 +28,10 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString @implementation AFImageCache +#if __IPHONE_OS_VERSION_MIN_REQUIRED +@synthesize imageScale = _imageScale; +#endif + + (AFImageCache *)sharedImageCache { static AFImageCache *_sharedImageCache = nil; static dispatch_once_t oncePredicate; @@ -39,11 +43,26 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString return _sharedImageCache; } +- (id)init { + self = [super init]; + if (self) { + #if __IPHONE_OS_VERSION_MIN_REQUIRED + self.imageScale = [[UIScreen mainScreen] scale]; + #endif + } + + return self; +} + #if __IPHONE_OS_VERSION_MIN_REQUIRED - (UIImage *)cachedImageForURL:(NSURL *)url cacheName:(NSString *)cacheName { - return [UIImage imageWithData:[self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]]; + UIImage *image = [UIImage imageWithData:[self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]]; + if (image) { + return [UIImage imageWithCGImage:[image CGImage] scale:self.imageScale orientation:image.imageOrientation]; + } + return image; } #elif __MAC_OS_X_VERSION_MIN_REQUIRED - (NSImage *)cachedImageForURL:(NSURL *)url