[Issue #103] Adding imageScale property on AFImageRequestOperation, to allow override of default behavior on iOS to automatically scale images for retina displays

This commit is contained in:
Mattt Thompson 2011-11-11 10:50:00 -06:00
parent f801bc453c
commit fdf217e8ab
2 changed files with 34 additions and 6 deletions

View file

@ -53,14 +53,27 @@
@private @private
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
UIImage *_responseImage; UIImage *_responseImage;
CGFloat _imageScale;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED #elif __MAC_OS_X_VERSION_MIN_REQUIRED
NSImage *_responseImage; NSImage *_responseImage;
#endif #endif
} }
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
/**
An image constructed from the response data. If an error occurs during the request, `nil` will be returned, and the `error` property will be set to the error.
*/
@property (readonly, nonatomic, retain) UIImage *responseImage; @property (readonly, nonatomic, retain) UIImage *responseImage;
/**
The scale factor used when interpreting the image data to construct `responseImage`. 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;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED #elif __MAC_OS_X_VERSION_MIN_REQUIRED
/**
An image constructed from the response data. If an error occurs during the request, `nil` will be returned, and the `error` property will be set to the error.
*/
@property (readonly, nonatomic, retain) NSImage *responseImage; @property (readonly, nonatomic, retain) NSImage *responseImage;
#endif #endif

View file

@ -45,6 +45,9 @@ static dispatch_queue_t image_request_operation_processing_queue() {
@implementation AFImageRequestOperation @implementation AFImageRequestOperation
@synthesize responseImage = _responseImage; @synthesize responseImage = _responseImage;
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@synthesize imageScale = _imageScale;
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
@ -173,6 +176,10 @@ static dispatch_queue_t image_request_operation_processing_queue() {
self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes]; self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes];
#if __IPHONE_OS_VERSION_MIN_REQUIRED
self.imageScale = [[UIScreen mainScreen] scale];
#endif
return self; return self;
} }
@ -184,16 +191,24 @@ static dispatch_queue_t image_request_operation_processing_queue() {
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
- (UIImage *)responseImage { - (UIImage *)responseImage {
if (!_responseImage && [self isFinished]) { if (!_responseImage && [self isFinished]) {
if ([[UIScreen mainScreen] scale] == 2.0) { CGImageRef imageRef = [[UIImage imageWithData:self.responseData] CGImage];
CGImageRef imageRef = [[UIImage imageWithData:self.responseData] CGImage]; self.responseImage = [UIImage imageWithCGImage:imageRef scale:self.imageScale orientation:UIImageOrientationUp];
self.responseImage = [UIImage imageWithCGImage:imageRef scale:2.0 orientation:UIImageOrientationUp];
} else {
self.responseImage = [UIImage imageWithData:self.responseData];
}
} }
return _responseImage; return _responseImage;
} }
- (void)setImageScale:(CGFloat)imageScale {
if (imageScale == _imageScale) {
return;
}
[self willChangeValueForKey:@"imageScale"];
_imageScale = imageScale;
[self didChangeValueForKey:@"imageScale"];
self.responseImage = nil;
}
#elif __MAC_OS_X_VERSION_MIN_REQUIRED #elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (NSImage *)responseImage { - (NSImage *)responseImage {
if (!_responseImage && [self isFinished]) { if (!_responseImage && [self isFinished]) {