Merge pull request #22 from steipete/master

Fixes warnings, Improves UIImageView loading helper
This commit is contained in:
Mattt Thompson 2011-09-12 18:17:57 -07:00
commit 8d9b7ace34
3 changed files with 40 additions and 30 deletions

View file

@ -23,6 +23,8 @@
#import "AFHTTPRequestOperation.h" #import "AFHTTPRequestOperation.h"
#import "AFNetworkActivityIndicatorManager.h" #import "AFNetworkActivityIndicatorManager.h"
#define AFHTTPMinContentLength 1024 * 1024 * 8
typedef enum { typedef enum {
AFHTTPOperationReadyState = 1, AFHTTPOperationReadyState = 1,
AFHTTPOperationExecutingState = 2, AFHTTPOperationExecutingState = 2,
@ -107,6 +109,14 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
static NSThread *_networkRequestThread = nil; static NSThread *_networkRequestThread = nil;
+ (void)networkRequestThreadEntryPoint:(id)object {
do {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] run];
[pool drain];
} while (YES);
}
+ (NSThread *)networkRequestThread { + (NSThread *)networkRequestThread {
static dispatch_once_t oncePredicate; static dispatch_once_t oncePredicate;
@ -118,14 +128,6 @@ static NSThread *_networkRequestThread = nil;
return _networkRequestThread; return _networkRequestThread;
} }
+ (void)networkRequestThreadEntryPoint:(id)object {
do {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] run];
[pool drain];
} while (YES);
}
+ (id)operationWithRequest:(NSURLRequest *)urlRequest + (id)operationWithRequest:(NSURLRequest *)urlRequest
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion
{ {
@ -246,16 +248,6 @@ static NSThread *_networkRequestThread = nil;
return YES; return YES;
} }
- (void)start {
if (self.isFinished) {
return;
}
self.state = AFHTTPOperationExecutingState;
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
}
- (void)operationDidStart { - (void)operationDidStart {
self.connection = [[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO] autorelease]; self.connection = [[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO] autorelease];
@ -269,6 +261,16 @@ static NSThread *_networkRequestThread = nil;
} }
- (void)start {
if (self.isFinished) {
return;
}
self.state = AFHTTPOperationExecutingState;
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
}
- (void)cancel { - (void)cancel {
self.isCancelled = YES; self.isCancelled = YES;
@ -297,7 +299,10 @@ didReceiveResponse:(NSURLResponse *)response
if (self.outputStream) { if (self.outputStream) {
[self.outputStream open]; [self.outputStream open];
} else { } else {
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8); NSUInteger contentLength = MAX(abs(response.expectedContentLength), 1024);
if (contentLength < AFHTTPMinContentLength) {
contentLength = AFHTTPMinContentLength;
}
self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength]; self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];
} }
} }

View file

@ -39,6 +39,6 @@
placeholderImage:(UIImage *)placeholderImage placeholderImage:(UIImage *)placeholderImage
imageSize:(CGSize)imageSize imageSize:(CGSize)imageSize
options:(AFImageRequestOptions)options options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image))block; block:(void (^)(UIImage *image, BOOL cacheUsed))block;
@end @end

View file

@ -84,9 +84,12 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
placeholderImage:(UIImage *)placeholderImage placeholderImage:(UIImage *)placeholderImage
imageSize:(CGSize)imageSize imageSize:(CGSize)imageSize
options:(AFImageRequestOptions)options options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image))block block:(void (^)(UIImage *image, BOOL cacheUsed))block
{ {
if (!url) { if (!url) {
// stop loading image
[self.imageRequestOperation cancel];
self.imageRequestOperation = nil;
return; return;
} }
@ -99,20 +102,22 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"imageRequestOperat
self.image = cachedImage; self.image = cachedImage;
if (block) { if (block) {
block(cachedImage); block(cachedImage, YES);
} }
} else { } else {
self.image = placeholderImage; self.image = placeholderImage;
self.imageRequestOperation = [AFImageRequestOperation operationWithRequest:request imageSize:imageSize options:options success:^(UIImage *image) { self.imageRequestOperation = [AFImageRequestOperation operationWithRequest:request imageSize:imageSize options:options success:^(UIImage *image) {
if (self.imageRequestOperation && ![self.imageRequestOperation isCancelled]) {
if (block) {
block(image, NO);
}
if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) { if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) {
self.image = image; self.image = image;
} else { } else {
self.image = placeholderImage; self.image = placeholderImage;
} }
if (block) {
block(image);
} }
}]; }];