Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 238 lines (204 sloc) 9.763 kb
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
1 // AFImageRequestOperation.m
2 //
3 // Copyright (c) 2011 Gowalla (http://gowalla.com/)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22
23 #import "AFImageRequestOperation.h"
24
da1fa38 Mattt Thompson Creating custom GCD queues for JSON and image request processing
mattt authored
25 static dispatch_queue_t af_image_request_operation_processing_queue;
26 static dispatch_queue_t image_request_operation_processing_queue() {
27 if (af_image_request_operation_processing_queue == NULL) {
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
28 af_image_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.image-request.processing", 0);
da1fa38 Mattt Thompson Creating custom GCD queues for JSON and image request processing
mattt authored
29 }
30
31 return af_image_request_operation_processing_queue;
32 }
33
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
34 @interface AFImageRequestOperation ()
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
35 #if __IPHONE_OS_VERSION_MIN_REQUIRED
f1b3101 Mattt Thompson First pass at converting to ARC
mattt authored
36 @property (readwrite, nonatomic) UIImage *responseImage;
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
37 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
38 @property (readwrite, nonatomic, retain) NSImage *responseImage;
39 #endif
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
40 @end
41
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
42 @implementation AFImageRequestOperation
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
43 @synthesize responseImage = _responseImage;
fdf217e Mattt Thompson [Issue #103] Adding imageScale property on AFImageRequestOperation, to a...
mattt authored
44 #if __IPHONE_OS_VERSION_MIN_REQUIRED
45 @synthesize imageScale = _imageScale;
46 #endif
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
47
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
48 #if __IPHONE_OS_VERSION_MIN_REQUIRED
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
49 + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
50 success:(void (^)(UIImage *image))success
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
51 {
f02c501 Mattt Thompson [Issue #347] Removing deprecated cacheName parameter in AFImageRequestOp...
mattt authored
52 return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, UIImage *image) {
50014af Mattt Thompson Refactoring image request API to remove unnecessary configuration, and f...
mattt authored
53 if (success) {
54 success(image);
55 }
56 } failure:nil];
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
57 }
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
58 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
59 + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
60 success:(void (^)(NSImage *image))success
61 {
f02c501 Mattt Thompson [Issue #347] Removing deprecated cacheName parameter in AFImageRequestOp...
mattt authored
62 return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSImage *image) {
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
63 if (success) {
64 success(image);
65 }
66 } failure:nil];
67 }
68 #endif
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
69
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
70
71 #if __IPHONE_OS_VERSION_MIN_REQUIRED
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
72 + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
73 imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock
74 success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
75 failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
76 {
f1b3101 Mattt Thompson First pass at converting to ARC
mattt authored
77 AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
78 [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
79 if (success) {
80 UIImage *image = responseObject;
81 if (imageProcessingBlock) {
da7d1c8 Mattt Thompson [Issue #296] Dispatching image processing asynchronously to background q...
mattt authored
82 dispatch_async(image_request_operation_processing_queue(), ^(void) {
83 UIImage *processedImage = imageProcessingBlock(image);
84
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
85 dispatch_async(requestOperation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) {
da7d1c8 Mattt Thompson [Issue #296] Dispatching image processing asynchronously to background q...
mattt authored
86 success(operation.request, operation.response, processedImage);
87 });
88 });
89 } else {
90 success(operation.request, operation.response, image);
50014af Mattt Thompson Refactoring image request API to remove unnecessary configuration, and f...
mattt authored
91 }
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
92 }
93 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
94 if (failure) {
95 failure(operation.request, operation.response, error);
96 }
97 }];
98
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
99
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
100 return requestOperation;
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
101 }
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
102 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
103 + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
104 imageProcessingBlock:(NSImage *(^)(NSImage *))imageProcessingBlock
105 success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success
106 failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
107 {
f1b3101 Mattt Thompson First pass at converting to ARC
mattt authored
108 AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
109 [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
110 if (success) {
111 NSImage *image = responseObject;
112 if (imageProcessingBlock) {
da7d1c8 Mattt Thompson [Issue #296] Dispatching image processing asynchronously to background q...
mattt authored
113 dispatch_async(image_request_operation_processing_queue(), ^(void) {
114 NSImage *processedImage = imageProcessingBlock(image);
115
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
116 dispatch_async(requestOperation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) {
da7d1c8 Mattt Thompson [Issue #296] Dispatching image processing asynchronously to background q...
mattt authored
117 success(operation.request, operation.response, processedImage);
118 });
119 });
120 } else {
121 success(operation.request, operation.response, image);
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
122 }
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
123 }
124 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
125 if (failure) {
126 failure(operation.request, operation.response, error);
127 }
128 }];
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
129
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
130 return requestOperation;
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
131 }
132 #endif
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
133
134 - (id)initWithRequest:(NSURLRequest *)urlRequest {
135 self = [super initWithRequest:urlRequest];
136 if (!self) {
137 return nil;
138 }
5b7a2cb Mattt Thompson Using class method swizzling to dynamically modify class method values f...
mattt authored
139
fdf217e Mattt Thompson [Issue #103] Adding imageScale property on AFImageRequestOperation, to a...
mattt authored
140 #if __IPHONE_OS_VERSION_MIN_REQUIRED
141 self.imageScale = [[UIScreen mainScreen] scale];
142 #endif
143
561df45 Mattt Thompson Initial working implementation of new candidate class structure
mattt authored
144 return self;
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
145 }
146
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
147
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
148 #if __IPHONE_OS_VERSION_MIN_REQUIRED
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
149 - (UIImage *)responseImage {
b782991 Mattt Thompson Adding check for responseData having non-zero length for response object...
mattt authored
150 if (!_responseImage && [self.responseData length] > 0 && [self isFinished]) {
0e93cf0 Mattt Thompson [Issue #110] Fixing orientation issues by using UIImage imageOrientation...
mattt authored
151 UIImage *image = [UIImage imageWithData:self.responseData];
152
153 self.responseImage = [UIImage imageWithCGImage:[image CGImage] scale:self.imageScale orientation:image.imageOrientation];
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
154 }
155
156 return _responseImage;
157 }
fdf217e Mattt Thompson [Issue #103] Adding imageScale property on AFImageRequestOperation, to a...
mattt authored
158
159 - (void)setImageScale:(CGFloat)imageScale {
eafbfec Steven Fisher Fixes warning about using == on floats.
tewha authored
160 #pragma clang diagnostic push
161 #pragma clang diagnostic ignored "-Wfloat-equal"
fdf217e Mattt Thompson [Issue #103] Adding imageScale property on AFImageRequestOperation, to a...
mattt authored
162 if (imageScale == _imageScale) {
163 return;
164 }
eafbfec Steven Fisher Fixes warning about using == on floats.
tewha authored
165 #pragma clang diagnostic pop
fdf217e Mattt Thompson [Issue #103] Adding imageScale property on AFImageRequestOperation, to a...
mattt authored
166
167 _imageScale = imageScale;
168
169 self.responseImage = nil;
170 }
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
171 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
172 - (NSImage *)responseImage {
b782991 Mattt Thompson Adding check for responseData having non-zero length for response object...
mattt authored
173 if (!_responseImage && [self.responseData length] > 0 && [self isFinished]) {
bfb6b35 David Keegan Update the NSImage code to just use an NSBitmapImageRep for determining ...
kgn authored
174 // Ensure that the image is set to it's correct pixel width and height
175 NSBitmapImageRep *bitimage = [[NSBitmapImageRep alloc] initWithData:self.responseData];
f1b3101 Mattt Thompson First pass at converting to ARC
mattt authored
176 self.responseImage = [[NSImage alloc] initWithSize:NSMakeSize([bitimage pixelsWide], [bitimage pixelsHigh])];
bfb6b35 David Keegan Update the NSImage code to just use an NSBitmapImageRep for determining ...
kgn authored
177 [self.responseImage addRepresentation:bitimage];
53d61e7 Mattt Thompson Adding pre-processor directives to conditionally compile Mac equivalent ...
mattt authored
178 }
179
180 return _responseImage;
181 }
182 #endif
ccdc5f2 Mattt Thompson Approaching a reasonable design for restructuring AFHTTPRequestOperation...
mattt authored
183
edb0830 Mattt Thompson Removing references to AFHTTPClientOperation protocol in documentation
mattt authored
184 #pragma mark - AFHTTPRequestOperation
c2be31d Mattt Thompson First implementation of protocol for AFHTTPClient to register classes to...
mattt authored
185
8bfee9e Mattt Thompson Refactoring +canProcessRequest: and content type acceptability
mattt authored
186 + (NSSet *)acceptableContentTypes {
187 return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil];
188 }
189
190 + (BOOL)canProcessRequest:(NSURLRequest *)request {
191 static NSSet * _acceptablePathExtension = nil;
192 static dispatch_once_t onceToken;
193 dispatch_once(&onceToken, ^{
194 _acceptablePathExtension = [[NSSet alloc] initWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil];
195 });
196
197 return [_acceptablePathExtension containsObject:[[request URL] pathExtension]] || [super canProcessRequest:request];
198 }
199
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
200 - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
201 failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
202 {
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
203 #pragma clang diagnostic push
204 #pragma clang diagnostic ignored "-Warc-retain-cycles"
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
205 self.completionBlock = ^ {
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
206 if ([self isCancelled]) {
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
207 return;
208 }
209
210 dispatch_async(image_request_operation_processing_queue(), ^(void) {
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
211 if (self.error) {
b723e5b Mattt Thompson Removing AFInternal category, opting instead to inline success and failu...
mattt authored
212 if (failure) {
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
213 dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
214 failure(self, self.error);
b723e5b Mattt Thompson Removing AFInternal category, opting instead to inline success and failu...
mattt authored
215 });
216 }
30a5cea Peter Steinberger Added methods to simplify calling the success/failure handlers.
steipete authored
217 } else {
b723e5b Mattt Thompson Removing AFInternal category, opting instead to inline success and failu...
mattt authored
218 if (success) {
ac20e02 Mattt Thompson Constructing image in background queue
mattt authored
219 #if __IPHONE_OS_VERSION_MIN_REQUIRED
220 UIImage *image = nil;
221 #elif __MAC_OS_X_VERSION_MIN_REQUIRED
222 NSImage *image = nil;
223 #endif
224
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
225 image = self.responseImage;
ac20e02 Mattt Thompson Constructing image in background queue
mattt authored
226
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
227 dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
228 success(self, image);
b723e5b Mattt Thompson Removing AFInternal category, opting instead to inline success and failu...
mattt authored
229 });
230 }
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
231 }
232 });
fefd1a7 Peter Steinberger don't use __weak for completionBlocks. (Issue #414)
steipete authored
233 };
234 #pragma clang diagnostic pop
5b5b32c Mattt Thompson Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess...
mattt authored
235 }
236
d87b783 Mattt Thompson Porting over changes for 0.3.0 release
mattt authored
237 @end
Something went wrong with that request. Please try again.