2014-09-27 19 views
0

只有當我開始滾動UITableView時,從url下載的圖像纔會在UITableViewCell中顯示圖像。現在我使用以下代碼從url加載圖片:只有當我滾動表格視圖時,纔會加載來自UITableViewCell中url的圖像

NSURL* url = [NSURL URLWithString:imageURL]; 
    NSURLRequest* request = [NSURLRequest requestWithURL:url]; 


[NSURLConnection sendAsynchronousRequest:request 
     queue:[NSOperationQueue mainQueue] 
     completionHandler:^(NSURLResponse * response, 
      NSData * data, 
      NSError * error) { 
    if (!error){ 
      NSImage* image = [[NSImage alloc] initWithData:data]; 
     // do whatever you want with image 
    } 

}]; 

在此先感謝。

+0

你從哪裏調用這段代碼?你可以添加你的tableView更新方法嗎? – Craig 2014-09-27 07:10:53

+0

我在單元格中爲indexpath中的行使用此代碼。 – Arun 2014-09-27 07:12:29

回答

0

您不應該在- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath中獲取圖像,您應該創建一個作爲後備數據存儲的實用程序類。隨着代碼的立場,您可以設想在單元格滾動進出視圖時多次重新請求圖像。

0

我強烈建議SDWebImage,這是非常簡單和強大。

您可以使用setImageWithURL類別,只需設置imageView的圖像。例如

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];` 

而且你可以簡單地通過[[SDImageCache sharedImageCache] storeImage:myImage forKey:myImageCacheKey]保存圖像緩存,只需[[SDImageCache sharedImageCache] queryDiskCacheForKey:myImageCacheKey done:^doneBlock]查詢您的圖像緩存;

非常簡單而強大,更多詳情請查看這裏SDWebImage;

相關問題