AFNetworking + UIImage首先下载低分辨率图像 [英] AFNetworking+UIImage Downloading Low-Resolution Image First

查看:84
本文介绍了AFNetworking + UIImage首先下载低分辨率图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用+ UIImage setImageWithURL:方法时遇到了一些麻烦。我想要做的是首先发送缩略图图像的异步请求。这个图像非常小,所以它应该很快加载。我还想发送另一个异步请求来下载图像的高分辨率版本,因为加载需要更长的时间。

I am having some troubles using the +UIImage setImageWithURL: method. What I would like to do is first send an asynchronous request for a thumbnail image. This image is very small so it should load fairly quickly. I also want to send another asynchrous request to download the hi-res version of the image since it takes a little longer to load.

我认为它会是这样的:

NSURL *thumbURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.server.com/uploads/thumbs/%@.png", name]];
[imageView setImageWithURL:thumbURL placeholderImage:[UIImage imageNamed:@"PHImage.png"]];

NSURL *hiRezURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.server.com/uploads/%@.png", name]];
[imageView setImageWithURL:hiRezURL];

但这只是加载高分辨率图像并且耗时太长。有什么建议吗?

but that just loads the hi-res image and takes too long. Any suggestions?

推荐答案

如果你看一下 UIImageView + AFNetworking.m的实现你看它声明了一个属性:

If you take a look at the implementation of UIImageView+AFNetworking.m you see that it declares a property:

@property (readwrite, nonatomic, retain, setter = af_setImageRequestOperation:) AFImageRequestOperation *af_imageRequestOperation;

首先是 - (void)setImageWithURL:(NSURL *) url placeholderImage:(UIImage *)placeholderImage 方法(它实际上首先调用 -setImageWithURLRequest:placeholderImage:success:failure:),是 [self cancelImageRequestOperation]; ,实际上取消了类属性的操作, af_imageRequestOperation

and the first thing that - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage method does (it actually first calls -setImageWithURLRequest:placeholderImage:success:failure:), is [self cancelImageRequestOperation]; and that actually cancels the operation of the class property, af_imageRequestOperation.

这意味着,当您再次(在第3行)调用该方法时,它会取消第1行的操作。

That means, when you call the method again (on your 3rd) line, it cancels the operation of the 1st line.

更好的方法是如果您使用 + imageRequestOperationWithRequest:imageProcessingBlock:success:failure:并在成功块中设置图像视图的图像。我想如果你使用这种方法,你还需要在之后调用 [操作开始];

A better approach would be if you use + imageRequestOperationWithRequest:imageProcessingBlock:success:failure: and set the image of image view in the success block. I think if you use this method, you need also to call [operation start]; afterwards.

希望它帮助。

这篇关于AFNetworking + UIImage首先下载低分辨率图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆