0

I'm trying to add UIImage objects into an array but they are not being added. Please help

my code:

imageArray = [[NSMutableArray alloc] init];
arry = [[NSMutableArray alloc] init];

[arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6291.jpg"];
[arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6290.jpg"];

//Fetch images from web and store in another array
for (int i=0; i<[arry count]; i++)
{
    NSString *string=[arry objectAtIndex:i];
    NSURL *url=[NSURL URLWithString:string];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           if ( !error )
                           {
                               UIImage *image = [[UIImage alloc] initWithData:data];

                               //Store image in imageArray
                               [imageArray insertObject:image atIndex:i];

                           } else{

                           }
                       }];
}

NSLog(@"%lu",(unsigned long)[imageArray count]); //this line is always showing 0 as count

When I'm printing the count of the array containing UIImage objects, its showing 0.

(I actually want to download images at once in array, then show it un UIImageView as slideshow).

Please guide me. Thank you!

3
  • You can first convert that image into NSData & can then add that data in array. Commented Jun 14, 2015 at 11:01
  • @RajatDeepSingh 1. Note that the received images are NSData objects. 2. There is no problem with saving the NSImage objects in an NSMutableArray, it is only the pointers that are in the array. Commented Jun 14, 2015 at 11:05
  • @zaph , Please check my comment to your answer below. Commented Jun 14, 2015 at 11:12

1 Answer 1

1

There are several problems.

  1. [[NSMutableArray alloc] init] does not allocate memory and items can not be added beyond the current size. [NSMutableArray arrayWithCapacity:size] does allocate the memory and objects can be inserted at any index up to and including the current size. See the NSMutableArray documentation.

  2. Immediately after the sendAsynchronousRequest block no images have ben downloaded, that will happen as the downloads complete.

  3. Starting a large number of downloads all at once can present performance problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I would like to inform that there won't be any large numbers of downloads at once because 1. images will be of small sizes, 2. only 2 - 4 images will be there. Secondly, I've also tried using SDWebImageManager but ran into a problem there , so I thought going this simpler way. Can you elaborate a little on your 1st point? And yes you're right about point # 2. But then where should I add them in my array? Regards
WRT point 1, change the allocation method of imageArray to arrayWithCapacity:. You are adding the images at a good point, it is just that they will be available at a later time. As they actually are received you can do something in the block, fire a notification or something based on your usage of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.