AFNetworking+UIImage Downloading Low-Resolution Im

2019-09-01 06:18发布

问题:

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.