Declared in UIButton+AFNetworking.h

Overview

This category adds methods to the UIKit framework’s UIButton class. The methods in this category provide support for loading remote images and background images asynchronously from a URL.

Tasks

Setting Image

  • – setImageForState:withURL:

    Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

  • – setImageForState:withURL:placeholderImage:

    Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

  • – setImageForState:withURLRequest:placeholderImage:success:failure:

    Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

Setting Background Image

Canceling Image Loading

Instance Methods

cancelBackgroundImageRequestOperation

- (void)cancelBackgroundImageRequestOperation
Discussion

Cancels any executing background image operation for the receiver, if one exists.

Declared In

UIButton+AFNetworking.h

cancelImageRequestOperation

- (void)cancelImageRequestOperation
Discussion

Cancels any executing image operation for the receiver, if one exists.

Declared In

UIButton+AFNetworking.h

setBackgroundImageForState:withURL:

- (void)setBackgroundImageForState:(UIControlState)state withURL:(NSURL *)url
Discussion

Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous background image request for the receiver will be cancelled.

If the background image is cached locally, the background image is set immediately, otherwise the specified placeholder background image will be set immediately, and then the remote background image will be set once the request is finished.

Parameters

state

The control state.

url

The URL used for the background image request.

Declared In

UIButton+AFNetworking.h

setBackgroundImageForState:withURL:placeholderImage:

- (void)setBackgroundImageForState:(UIControlState)state withURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
Discussion

Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

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.

Parameters

state

The control state.

url

The URL used for the background image request.

placeholderImage

The background image to be set initially, until the background image request finishes. If nil, the button will not change its background image until the background image request finishes.

Declared In

UIButton+AFNetworking.h

setBackgroundImageForState:withURLRequest:placeholderImage:success:failure:

- (void)setBackgroundImageForState:(UIControlState)state withURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage success:(void ( ^ ) ( NSHTTPURLResponse *response , UIImage *image ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

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.

If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with setBackgroundImage:forState: is applied.

Parameters

state

The control state.

urlRequest

The URL request used for the image request.

placeholderImage

The background image to be set initially, until the background image request finishes. If nil, the button will not change its background image until the background image request finishes.

Declared In

UIButton+AFNetworking.h

setImageForState:withURL:

- (void)setImageForState:(UIControlState)state withURL:(NSURL *)url
Discussion

Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

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.

Parameters

state

The control state.

url

The URL used for the image request.

Declared In

UIButton+AFNetworking.h

setImageForState:withURL:placeholderImage:

- (void)setImageForState:(UIControlState)state withURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
Discussion

Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

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.

Parameters

state

The control state.

url

The URL used for the image request.

placeholderImage

The image to be set initially, until the image request finishes. If nil, the button will not change its image until the image request finishes.

Declared In

UIButton+AFNetworking.h

setImageForState:withURLRequest:placeholderImage:success:failure:

- (void)setImageForState:(UIControlState)state withURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage success:(void ( ^ ) ( NSHTTPURLResponse *response , UIImage *image ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled.

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.

If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with setImage:forState: is applied.

Parameters

state

The control state.

urlRequest

The URL request used for the image request.

placeholderImage

The image to be set initially, until the image request finishes. If nil, the button will not change its image until the image request finishes.

success

A block to be executed when the image request operation finishes successfully. This block has no return value and takes two arguments: the server response and the image. If the image was returned from cache, the request and response parameters will be nil.

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 a single argument: the error that occurred.

Declared In

UIButton+AFNetworking.h