Question

UPDATE

While trying to find a solution I've happen to notice the following two questions:

I'll investigate using these two post and will update if it came to be helpful.

ORIGINAL POST

I got this horizontal feed that contain 1 item per page. Each item contain some textual info and a background image that I'm pulling from a url.

Since the background images are in high quality it might take some time to download it, so I figured out I'll need a placeholder. The placeholder I would like to use is an extreme blurred, really low quality version of the background image which means I need a variation of the beloved AFNetworking method: [bg setImageWithURL: placeholderImage:] - something that will know how to accept a placeholder that comes from a url (making the assumption that the placeholder image weigh MUCH less then the original size one).

To sum up, my question is: How to get an image from a url and use a placeholder, where the placeholder image is from a url as well?

Things I tried:

  • Creating another UIImageView *placeholder and use the [placeholder setImageWithURL:] method on it with the url of the placeholder, then using the placeholder.image as the placeholder of the [bg setImageWithURL: placeholderImage:] call.

  • Using the [placeHolderImage setImageWithURLRequest:placeholderImage:success:failure:] method, and in the success block calling the [bg setImageWithURL:] method.

Was it helpful?

Solution

Apparently I was on the right track and due to lack of attention it didn't work. (had problem with retain cycle) Using the two links I provided in the question, I eventually managed to solve the problem by using the setImageWithURLRequest:placeholderImage:success:failure: for the placeholder, and on the success block, make another use of the setImageWithURLRequest:placeholderImage:success:failure: method - this time for the original image.

I'll provide a code snippet of my solution - might help someone in the future:

    //Protect against retain cycle
    __weak UIImageView *weakBg = bg;

    //get the placeholder - note the placeholderImage parameter is nil (I don't need a placeholder to the placeholder
    [bg setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:placeholderURL]]
              placeholderImage: nil
                       success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                           UIImageView *strongBg = weakBg; // Make local strong reference to protect against race conditions
                           if (!strongBg) return;

                           //Protect against retain cycle
                           __weak UIImageView *weakBg = strongBg;

                         //Get the original bg with the low quality bg as placeholder
                           [strongBg setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:originalBgImageURL]]
                                           placeholderImage:image
                                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                               UIImageView *strongBg = weakBg; // Make local strong reference to protect against race conditions
                               if (!strongBg) return;

                               //Do stuff

                           } failure:NULL];
                       } failure:NULL];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top