1

I have an array or urls which points to images present on the server. Now I want to display the images in a scrollview with 4 images on each row. I am thinking about using NSOperationQueue and NSInvocationOperation to download the images asynchronously. I am using following url as reference:
http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation

But I am not sure how will I download multiple images. Do I have to run a for loop with multiple NSInvocationOperation objects and add it to NSOperationQueue object?

I am looking for any guidance to achieve my task.

EDIT: I have done following but NSOperationQueue is not able to call the NSInvocationOperation objects

NSOperationQueue *queue = [NSOperationQueue new];
for(int i = 0;i<rowcount;i++){
    for(int j =0; j<4;j++){
        UIButton *btnAlbum = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 72, 72)];
        [btnAlbum setBackgroundImage:[UIImage imageNamed:@"placeHolder.png"] forState:UIControlStateNormal];
        btnAlbum.tag = count+100;
        //[btnAlbum addTarget:self action:@selector(viewAlbum:) forControlEvents:UIControlEventTouchUpInside];
        [scrlvw addSubview:btnAlbum];
        //[btnAlbum release];
        x = x + 80;
        count++;

        NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
                                            initWithTarget:self
                                            selector:@selector(loadImage) 
                                            object:nil];

        [queue addOperation:operation]; 
        [operation release];
    }
    x = 0;
    y = y +80;
}

Thanks
Pankaj

pankaj
  • 7,878
  • 16
  • 69
  • 115
  • @Priya I ended up using ASIHTTPRequest, here, but it has been deprecated now, you can use Alamofire now – pankaj May 27 '17 at 10:06

3 Answers3

1

yes -- for each image that you want to download, you create a NSInvocationOperation which, when executed, will call the routine that downloads the image. The NSInvocationOperation is added to the queue, which is responsible for executing the NSInvocationOperation on it's own thread.

You only need one queue, but you need a new NSInvocationOperation for each download.

elijah
  • 2,904
  • 1
  • 17
  • 21
1

ASIHTTPRequest came to rescue me and I was able to perform my task with ease.

pankaj
  • 7,878
  • 16
  • 69
  • 115
0

Why not use blocks and Grand Central Dispatch to load these images? Check out this post and see if that will work for you.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62