Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
[Issue #103] Adding imageScale property on AFImageRequestOperation, t…
Browse files Browse the repository at this point in the history
…o allow override of default behavior on iOS to automatically scale images for retina displays
  • Loading branch information
mattt committed Nov 11, 2011
1 parent f801bc4 commit fdf217e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
13 changes: 13 additions & 0 deletions AFNetworking/AFImageRequestOperation.h
Expand Up @@ -53,14 +53,27 @@
@private
#if __IPHONE_OS_VERSION_MIN_REQUIRED
UIImage *_responseImage;
CGFloat _imageScale;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
NSImage *_responseImage;
#endif
}

#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;

/**
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

/**
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;
#endif

Expand Down
27 changes: 21 additions & 6 deletions AFNetworking/AFImageRequestOperation.m
Expand Up @@ -45,6 +45,9 @@ + (NSSet *)defaultAcceptablePathExtensions;

@implementation AFImageRequestOperation
@synthesize responseImage = _responseImage;
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@synthesize imageScale = _imageScale;
#endif

#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
Expand Down Expand Up @@ -173,6 +176,10 @@ - (id)initWithRequest:(NSURLRequest *)urlRequest {

self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes];

#if __IPHONE_OS_VERSION_MIN_REQUIRED
self.imageScale = [[UIScreen mainScreen] scale];
#endif

return self;
}

Expand All @@ -184,16 +191,24 @@ - (void)dealloc {
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- (UIImage *)responseImage {
if (!_responseImage && [self isFinished]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
CGImageRef imageRef = [[UIImage imageWithData:self.responseData] CGImage];
self.responseImage = [UIImage imageWithCGImage:imageRef scale:2.0 orientation:UIImageOrientationUp];
} else {
self.responseImage = [UIImage imageWithData:self.responseData];
}
CGImageRef imageRef = [[UIImage imageWithData:self.responseData] CGImage];
self.responseImage = [UIImage imageWithCGImage:imageRef scale:self.imageScale orientation:UIImageOrientationUp];
}

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
- (NSImage *)responseImage {
if (!_responseImage && [self isFinished]) {
Expand Down

0 comments on commit fdf217e

Please sign in to comment.