Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Issue #103] Adding imageScale property on AFImageRequestOperation, t…

…o allow override of default behavior on iOS to automatically scale images for retina displays
  • Loading branch information...
commit fdf217e8abc4c2bf9479791d79d7a33e68bffb1e 1 parent f801bc4
@mattt mattt authored
View
13 AFNetworking/AFImageRequestOperation.h
@@ -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
View
27 AFNetworking/AFImageRequestOperation.m
@@ -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
@@ -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;
}
@@ -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]) {
Please sign in to comment.
Something went wrong with that request. Please try again.