Added scaling while constructing image from cached data.

This commit is contained in:
Štěpán Petrů 2011-12-10 19:00:49 +01:00
parent 049fef5888
commit 4c5bdbd7de
2 changed files with 27 additions and 1 deletions

View file

@ -32,6 +32,13 @@
*/ */
@interface AFImageCache : NSCache @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. Returns the shared image cache object for the system.

View file

@ -28,6 +28,10 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
@implementation AFImageCache @implementation AFImageCache
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@synthesize imageScale = _imageScale;
#endif
+ (AFImageCache *)sharedImageCache { + (AFImageCache *)sharedImageCache {
static AFImageCache *_sharedImageCache = nil; static AFImageCache *_sharedImageCache = nil;
static dispatch_once_t oncePredicate; static dispatch_once_t oncePredicate;
@ -39,11 +43,26 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
return _sharedImageCache; 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 #if __IPHONE_OS_VERSION_MIN_REQUIRED
- (UIImage *)cachedImageForURL:(NSURL *)url - (UIImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName 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 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (NSImage *)cachedImageForURL:(NSURL *)url - (NSImage *)cachedImageForURL:(NSURL *)url