From eebc763f7f3987bdd222357ca70e0df6ec9d9e63 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 22 Sep 2011 11:30:13 -0500 Subject: [PATCH] Adding documentation to UIImageView category Changing API for setImageWithURLRequest:placeholderImage:success to setImageWithURLRequest:placeholderImage:success:failure --- AFNetworking/UIImageView+AFNetworking.h | 36 ++++++++++++++++++++++++- AFNetworking/UIImageView+AFNetworking.m | 19 ++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/AFNetworking/UIImageView+AFNetworking.h b/AFNetworking/UIImageView+AFNetworking.h index 16f8889..4b6d72b 100644 --- a/AFNetworking/UIImageView+AFNetworking.h +++ b/AFNetworking/UIImageView+AFNetworking.h @@ -23,16 +23,50 @@ #import #import "AFImageRequestOperation.h" +/** + This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL. + */ @interface UIImageView (AFNetworking) +/** + Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL, and sets it the request is finished. If the image is cached locally, the image is set immediately, otherwise, the image is set once the request is finished. + + @discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` + + @param url The URL used for the image request. + */ - (void)setImageWithURL:(NSURL *)url; +/** + Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + @param url The URL used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. + + @discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` + + @warning If `placeholderImage` is specified, the remote image will be resized to the dimensions of the placeholder image before being set. + */ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage; +/** + Creates and enqueues an image request operation, which asynchronously downloads the image with the specified url request object. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + @param urlRequest The url request used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. + @param success A block to be executed when the image request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `image/png`). This block has no return value and takes three arguments, the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the request and response parameters will be `nil`. + @param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments, the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + + + @discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` + + @warning If `placeholderImage` is specified, the remote image will be resized to the dimensions of the placeholder image before being set. + */ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage - success:(void (^)(UIImage *image, BOOL cacheUsed))success; + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response,UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; - (void)cancelImageRequestOperation; diff --git a/AFNetworking/UIImageView+AFNetworking.m b/AFNetworking/UIImageView+AFNetworking.m index dcd3a50..32a3826 100644 --- a/AFNetworking/UIImageView+AFNetworking.m +++ b/AFNetworking/UIImageView+AFNetworking.m @@ -102,12 +102,13 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp [request setHTTPShouldHandleCookies:NO]; [request setHTTPShouldUsePipelining:YES]; - [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil]; + [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil]; } - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage - success:(void (^)(UIImage *image, BOOL cacheUsed))success + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response,UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure { if (![urlRequest URL] || (![self.imageRequestOperation isCancelled] && [[urlRequest URL] isEqual:self.imageRequestOperation.request.URL])) { return; @@ -125,7 +126,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp self.image = cachedImage; if (success) { - success(cachedImage, YES); + success(nil, nil, cachedImage); } } else { self.image = placeholderImage; @@ -140,7 +141,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp if (self.imageRequestOperation && ![self.imageRequestOperation isCancelled]) { dispatch_async(dispatch_get_main_queue(), ^{ if (success) { - success(image, NO); + success(request, response, image); } if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) { @@ -152,11 +153,15 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { self.imageRequestOperation = nil; + + dispatch_async(dispatch_get_main_queue(), ^{ + if (failure) { + failure(request, response, error); + } + }); }]; - if ([urlRequest cachePolicy] != NSURLCacheStorageNotAllowed) { - [[[self class] sharedImageRequestOperationQueue] addOperation:self.imageRequestOperation]; - } + [[[self class] sharedImageRequestOperationQueue] addOperation:self.imageRequestOperation]; } }