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!
There are several problems.
[[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 theNSMutableArray
documentation.Immediately after the
sendAsynchronousRequest
block no images have ben downloaded, that will happen as the downloads complete.Starting a large number of downloads all at once can present performance problem.