Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to download and store images from an array of URLs using AFNetworking?
    primarykey
    data
    text
    <p>I am using <code>"UIImageView+AFNetworking.h"</code>, and my goal is to take a NSArray of URL called <code>imageURLArray</code>, download all the pictures, put them in a scroll view with page controls.</p> <p>To do this, I am downloading the pictures one at a time in the following loop, and have a previously allocated/initialized mutable array <code>downloadImageArray</code> where I stored the downloaded picture:</p> <pre><code>for (int i = 0; i &lt; [imageURLArray count]; i++){ NSURL *testURL = [imageURLArray objectAtIndex:i]; NSURLRequest *testURLRequest = [[NSURLRequest alloc] initWithURL:testURL]; [placeHolderImageView setImageWithURLRequest:testURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { NSLog(@"success"); NSLog(@"The image is %@", image); [downloadedImageArray addObject:image]; //! the exception is thrown here - it says the object is NULL } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { NSLog(@"failed to download"); }]; } NSLog(@"Count of images is %i", [downloadedImageArray count]) //this always returns "0" </code></pre> <p>As comment in the line where I add the object to <code>downloadedImageArray</code>, I get the exception that the object added to the mutable array is NULL. I suspect that this is because the image hasn't completed downloading before the next URL is put in place and is being asked to download again. Also, after the loop, when I get a count of <code>downloadedImageArray</code>, the count returns 0.</p> <p>My questions are:</p> <ol> <li>How would be able to ensure that one image has completed downloading before looping through to the next request to download the next image?</li> <li>As a design choice, does it make sense to download all the images first before adding them to my scroll view (the width of each image is set fixed), or does it make more sense to download as they are being scrolled to? If so, how could I implement this? Most of the time <code>imageURLArray</code> has no more than 4 set of URLs.</li> </ol> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload