Adding documentation to UIImageView category

Changing API for setImageWithURLRequest:placeholderImage:success to setImageWithURLRequest:placeholderImage:success:failure
This commit is contained in:
Mattt Thompson 2011-09-22 11:30:13 -05:00
parent c84ca99269
commit eebc763f7f
2 changed files with 47 additions and 8 deletions

View file

@ -23,16 +23,50 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "AFImageRequestOperation.h" #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) @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; - (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 - (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage; 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 - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage 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; - (void)cancelImageRequestOperation;

View file

@ -102,12 +102,13 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp
[request setHTTPShouldHandleCookies:NO]; [request setHTTPShouldHandleCookies:NO];
[request setHTTPShouldUsePipelining:YES]; [request setHTTPShouldUsePipelining:YES];
[self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil]; [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
} }
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage 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])) { if (![urlRequest URL] || (![self.imageRequestOperation isCancelled] && [[urlRequest URL] isEqual:self.imageRequestOperation.request.URL])) {
return; return;
@ -125,7 +126,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp
self.image = cachedImage; self.image = cachedImage;
if (success) { if (success) {
success(cachedImage, YES); success(nil, nil, cachedImage);
} }
} else { } else {
self.image = placeholderImage; self.image = placeholderImage;
@ -140,7 +141,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp
if (self.imageRequestOperation && ![self.imageRequestOperation isCancelled]) { if (self.imageRequestOperation && ![self.imageRequestOperation isCancelled]) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if (success) { if (success) {
success(image, NO); success(request, response, image);
} }
if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) { if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) {
@ -152,13 +153,17 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp
} }
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
self.imageRequestOperation = nil; 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];
} }
} }
}
- (void)cancelImageRequestOperation { - (void)cancelImageRequestOperation {
[self.imageRequestOperation cancel]; [self.imageRequestOperation cancel];