2012-01-11 52 views
1

我有一個數組或URL指向圖像出現在服務器上。現在我想在滾動視圖中顯示每行4個圖像的圖像。我想使用NSOperationQueue和NSInvocationOperation異步下載圖像。我使用以下網址作爲參考:
http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation異步使用NSOperationQueue從網上加載多個圖像

但我不知道我將如何下載多個圖像。我是否必須使用多個NSInvocationOperation對象運行for循環並將其添加到NSOperationQueue對象?

我正在尋找任何指導來實現我的任務。

編輯: 我已經做以下,但NSOperationQueue不能調用NSInvocationOperation對象

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; 
} 

感謝
潘卡

+0

@Priya我在這裏結束了使用ASIHTTPRequest,但現在已經被棄用,現在可以使用Alamofire – pankaj 2017-05-27 10:06:53

回答

1

是 - 爲您要下載的每個圖像,你創建一個NSInvocationOperation,它在執行時會調用下載圖像的例程。 NSInvocationOperation被添加到隊列中,該隊列負責在其自己的線程上執行NSInvocationOperation。

您只需要一個隊列,但每次下載都需要一個新的NSInvocationOperation。

0

爲什麼不使用塊和Grand Central Dispatch來加載這些圖像?看看this post,看看它是否適合你。