AFNetworking+UIImage Downloading Low-Resolution Image First

风流意气都作罢 提交于 2019-12-12 01:08:11

问题


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.

I assumed it would be something like:

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?


回答1:


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;

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.

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

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.

Hope it helps.



来源:https://stackoverflow.com/questions/11618523/afnetworkinguiimage-downloading-low-resolution-image-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!