I am using "UIImageView+AFNetworking.h"
, and my goal is to take a NSArray of URL called imageURLArray
, download all the pictures, put them in a scroll view with page controls.
To do this, I am downloading the pictures one at a time in the following loop, and have a previously allocated/initialized mutable array downloadImageArray
where I stored the downloaded picture:
for (int i = 0; i < [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"
As comment in the line where I add the object to downloadedImageArray
, 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 downloadedImageArray
, the count returns 0.
My questions are:
- How would be able to ensure that one image has completed downloading before looping through to the next request to download the next image?
- 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
imageURLArray
has no more than 4 set of URLs.
Thanks!