您的位置:首页 > 产品设计 > UI/UE

iOS_AFNetworking_UIImageView+AFNetworking(实现图片异步加载)

2015-09-23 22:01 609 查看
说明 :

UIImageView+AFNetworking 是AFNetworking中一个实现图片异步加载的类, 它是为系统中的UIImageView类添加的类目(Category), 这个类目中的方法为远程异步加载图片功能提供支持.

本文主要是针对UIImageView+AFNetworking的一些使用方法和在应用时需要注意的地方进行归纳, 供大家参考和自己学习.

程序代码中的 urlStr 和 placeholderImage 以自己工程中为准, 本文中使用MRC进行内存管理.

文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)

此文章由@河马流星锤编写, 经@春雨, @Scott 审核, 若转载此文章请注明出处和作者.

UIImageView+AFNetworking 的创建方法及相关知识点

核心API

class : UIImageView+AFNetworking

delegate : 无

涉及的API : (AFNetworking的官方API(英文)详见本章结尾)

/** 1. 通过指定的NSURL对象异步加载一个图片 */
- (void)setImageWithURL:(NSURL *)url

/** 通过指定的NSURL对象异步加载一个图片, 并在加载完成之前设置一个占位图片. */
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage

/** 2. 通过指定的NSRequest对象异步加载一个图片, 参数介绍详见本文结尾的API英文注释部分 */
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure

/** 3. 取消请求操作 */
- (void)cancelImageRequestOperation


功能实现

思路

导入AFNetworking工程文件.

引入UIKit+AFNetworking/UIImageView+AFNetworking.h头文件.

下载图片

取消图片下载(可选)

(1). 导入AFNetworking工程文件

/** 本文使用的AFNetworking版本为AFNetworking_v2.5.1. */


(2). 引入UIKit+AFNetworking/UIImageView+AFNetworking.h头文件

#import "UIKit+AFNetworking/UIImageView+AFNetworking.h"

/** 说明: UIImageView(AFNetworking)因为是在系统类上增加的类目不需要自己创建, 只要将AFNetworking工程文件拖入工程, 并在要使用页面位置引入头文件即可, 本文介绍使用的是添加到工程的两个文件夹中的文件夹"UIKit+AFNetworking"中, 引入"UIImageView+AFNetworking.h" */


(3). 实现图片异步加载

/* 创建NSURL对象 */
NSURL *urlStr = [NSURL URLWithString:@"http://img2.cache.netease.com/3g/2015/9/18/20150918195439dc844.jpg"];

/* 创建请求对象 */
NSURLRequest *request = [NSURLRequest requestWithURL:urlStr];

/* 创建一个imageView对象 */
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];


/**
* 方法一:
* 直接使用url字符串获取照片
* @param urlStr : NSURL对象
*/
[imageView setImageWithURL:urlStr];


/**
* 方法二:
* 直接使用URL(例如:http://img2.cache.netease.com/3g/2015/9/18/20150918195439dc844.jpg)异步加载图片, 照片出现之前添加一个占位的背景照片
* @param urlStr : NSURL对象
* @param placeholderImage : UIImage图片对象
*/
[imageView setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"discuss"]];


/**
* 方法三:
* 使用URLRequest对象获取照片, 照片在请求后的block块中返回一个UIImage参数用于赋值或其他作.
* 参数中得两个block块:分别在请求成功时执行 success:^{},
请求失败时执行 failure:^{}。
* @param request : NSURLRequest请求对象
* @param placeholderImage : UIImage图片对象
*/
[imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"discuss"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
/**
* 返回值参数
* @param request : NSURLRequest 请求对象
* @param response : NSHTTPURLResponse HTTP响应者对象(包含请求对象和图片信息)
* @param image : 加载成功图片UIImage对象(自带的size尺寸用于自适应高度计算)
*/
[imageView setImage:image];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
/**< @param error : NSError对象 加载错误的原因代号(例如 : 403) */
}];


/**
* 取消图片请求操作
* 可以直接取消本段代码块中在调用这个方法之前的所有异步加载操作(适当的地方慎重使用)
*/
[imageView cancelImageRequestOperation];


/* 将定义的imageView添加到ViewController中 */
[self.view addSubview:imageView];

[imageView release];


UIImageViewAFNetworking 的创建方法及相关知识点
核心API

功能实现

思路

1 导入AFNetworking工程文件

2 引入UIKitAFNetworkingUIImageViewAFNetworkingh头文件

3 实现图片异步加载

API 官方注释英文

API 官方注释(英文)

/**
* @discussion  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.

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.

By default, URL requests have a `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, use `setImageWithURLRequest:placeholderImage:success:failure:`.

*
* @param url The URL used for the image request.
*/

/** 方法 */
- (void)setImageWithURL:(NSURL *)url;


/**
* @discussion  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.

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.

By default, URL requests have a `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, use `setImageWithURLRequest:placeholderImage:success:failure:`.
*
* @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.
*/

/** 方法 */
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage;


/**
* @discussion  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.

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 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 <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. 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
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;


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

/** 方法 */
- (void)cancelImageRequestOperation;


/**
* Returns a cached image for the specififed request, if available.
*
* @param request The image request.
*
* @return The cached image.
*/

/** 方法 */
- (UIImage *)cachedImageForRequest:(NSURLRequest *)request;


/**
* Caches a particular image for the specified request.
*
* @param image The image to cache.
* @param request The request to be used as a cache key.
*/

/** 方法 */
- (void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)request;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: