2012-12-30 39 views
-1

可能重複:
Download image asynchronously如何下載多個圖像asyncronously

我想異步下載多個圖像。我有10個圖像陣列,我想在10 uiimageview,我做這樣的顯示這些圖片,但只顯示一個圖像

- (void)loadImage { 
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[myarray objectatindex:0]]]; 
    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; 
    [imageData release]; 
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO]; 
} 

The final method we need to create takes in the image to display and sets it on our image view. 

- (void)displayImage:(UIImage *)image { 
    [imageView setImage:image]; //UIImageView 
} 

所以,請你告訴我,我怎麼能顯示不同的多張圖片,而無需創建10不同的方法。

回答

1

請勿使用同步的NSDatainitWithContentsOfURL方法。

你可能想要考慮UIImageView + AFNetworking類別,你可以找到here,它增加了UIImageView異步檢索其內容的能力。

正如評論中的建議,你也可以看看SDWebImage,它基本上是一樣的。

+0

是的,這可能是最好的辦法,作爲替代方案,你可以使用SDWebImage,你可以找到https://github.com/rs/SDWebImage – mohacs

+0

我將如何使用該類 –

+0

@Adnan哪一個? –

0

只要把這些類放到你的項目中,你就必須導入一些類。 對於sdwebimage,你會得到一個uiimageview的方法,就像「loadimagefrom url:withplaceholderimage」,只是檢查一次,我不記得方法的名稱。

+0

很多錯誤,包括該sdwebimage文件,但我在這個文件中使用undeclare標識符_manager –

+0

很多錯誤你必須包括整個sdwebimage圖書館 – Exploring