diff --git a/AFNetworking+ImageActivityIndicator/AFNetworking+ImageActivityIndicator.h b/AFNetworking+ImageActivityIndicator/AFNetworking+ImageActivityIndicator.h index 2eff9db..006221f 100644 --- a/AFNetworking+ImageActivityIndicator/AFNetworking+ImageActivityIndicator.h +++ b/AFNetworking+ImageActivityIndicator/AFNetworking+ImageActivityIndicator.h @@ -27,4 +27,4 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#import +#import diff --git a/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.h b/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.h index f62992f..4b9c3c5 100644 --- a/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.h +++ b/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.h @@ -61,10 +61,29 @@ * By default, URL requests have an `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, yet still show an activity indicator, use `setImageWithURLRequest:usingActivityIndicatorStyle:success:failure:` * * @param url The URL used for the image request. - * @param style The style for the activity indicator + * @param style The style for the activity indicator. */ - (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style; +/** + * Asynchronously downloads an image from the specified URL, and sets it once the request is finished. Any previous image request for the receiver will be cancelled. Use + + * If the image is cached locally, the image is set immediately, otherwise the placeholder is shown and an activity indicator view will be added as a subview of the image view, centered, start animating, and then the remote image will be set once the request is finished. + + * By default, URL requests have an `Accept` header field value of "image / *", a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, yet still show an activity indicator, use `setImageWithURLRequest:usingActivityIndicatorStyle:success:failure:` + * + * @param url The URL used for the image request. + * @param style The style for the activity indicator. + * @param color The color of activity indicator. + * @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 failureImage The image to set if image request operation finishes unsuccessfully. + */ +- (void)setImageWithURL:(NSURL *)url + activityIndicatorStyle:(UIActivityIndicatorViewStyle)style + activityIndicatorColor:(UIColor *)color + placeholderImage:(UIImage *)placeholderImage + failureImage:(UIImage *)failureImage; + /** * Asynchronously downloads an image from the specified URL request, and sets it once the request is finished. Any previous image request for the receiver will be cancelled. @@ -73,14 +92,18 @@ * If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with `self.image = image` is applied. * * @param urlRequest The URL request used for the image request. + * @param style The style for the activity indicator. + * @param color The color of activity indicator. * @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 style The style for the activity indicator + * @param failureImage The image to set if image request operation finishes unsuccessfully. * @param success A block to be executed when the image request operation 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 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. */ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest - placeholderImage:(UIImage *)placeholderImage usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style + activityIndicatorColor:(UIColor *)color + placeholderImage:(UIImage *)placeholderImage + failureImage:(UIImage *)failureImage success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; diff --git a/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.m b/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.m index 84a5397..dd2ad2e 100644 --- a/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.m +++ b/AFNetworking+ImageActivityIndicator/UIImageView+AFNetworking_UIActivityIndicatorView.m @@ -65,24 +65,43 @@ - (void)removeActivityIndicatorView - (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style { [self setImageWithURLRequest:[NSURLRequest requestWithURL:url] - placeholderImage:nil usingActivityIndicatorStyle:style + activityIndicatorColor:nil + placeholderImage:nil + failureImage:nil success:nil failure:nil]; } +- (void)setImageWithURL:(NSURL *)url + activityIndicatorStyle:(UIActivityIndicatorViewStyle)style + activityIndicatorColor:(UIColor *)color + placeholderImage:(UIImage *)placeholderImage + failureImage:(UIImage *)failureImage; +{ + [self setImageWithURLRequest:[NSURLRequest requestWithURL:url] + usingActivityIndicatorStyle:style + activityIndicatorColor:color + placeholderImage:placeholderImage + failureImage:failureImage + success:nil + failure:nil]; +} + - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest - placeholderImage:(UIImage *)placeholderImage usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style + activityIndicatorColor:(UIColor *)color + placeholderImage:(UIImage *)placeholderImage + failureImage:(UIImage *)failureImage success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure { if (urlRequest.URL == nil) { - self.image = nil; + self.image = failureImage; return; } - [self af_addActivityIndicatorWithStyle:style]; + [self af_addActivityIndicatorWithStyle:style color:color]; __weak UIImageView *weakSelf = self; @@ -108,6 +127,8 @@ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest if (failure) { failure(request, response, error); + } else { + strongSelf = failureImage; } }]; } @@ -115,12 +136,13 @@ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest #pragma mark - Private Helpers - (void)af_addActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style + color:(UIColor *)color { UIActivityIndicatorView *activityIndicator = [self activityIndicatorView]; if (!activityIndicator) { - activityIndicator = [self af_createActivityIndicatorWithStyle:style]; + activityIndicator = [self af_createActivityIndicatorWithStyle:style color:color]; [self setActivityIndicatorView:activityIndicator]; if ([NSThread isMainThread]) { @@ -147,13 +169,18 @@ - (void)af_addActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style } - (UIActivityIndicatorView *)af_createActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style + color:(UIColor *)color { UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style]; activityIndicator.userInteractionEnabled = NO; activityIndicator.center = self.center; activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; - + + if (color) { + activityIndicator.color = color; + } + return activityIndicator; }