2011-12-05 28 views
4

我有以下代碼:(注:newsImageURLNSArray加載圖像到一個UITableViewCell的UIImageView的

NSString *imagesURL = @"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage02.png,http://aud.edu/images/newsimage03.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,"; 
newsImageURL = [[NSArray alloc] initWithArray:[AllNewsHeadLine componentsSeparatedByString:@","]]; 

我想用下面的代碼到這些圖像加載到單元格:

NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; 
cell.image = [UIImage imageWithData:imageData]; 

圖像加載罰款當我使用此行:

cell.image = [UIImage imageNamed:@"imagename.png"]; 

什麼我做錯了嗎?

+0

代碼會發生什麼情況?加載一些圖像,或者您每次都會在圖像應該放置的地方留下空白區域? – reddersky

+0

當我嘗試從URL中加載它的圖像空間消失,沒有圖像顯示,沒有任何反應basicaly。但是當我使用'imageNamed'加載它時,它加載正常 –

+0

在您將圖像數據加載到NSData對象imageData中之後,您是否檢查過它是否實際包含任何數據[imageData length]; – reddersky

回答

9

您應該使用支持緩存,默認佔位符和圖像延遲加載的現有框架。

https://github.com/rs/SDWebImage是一個很好的和簡單的框架

#import "UIImageView+WebCache.h" 

... 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Here we use the new provided setImageWithURL: method to load the web image 
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    cell.textLabel.text = @"My Text"; 
    return cell; 
} 
+0

謝謝,這就是我想要的。 –

+3

他問爲什麼上面的代碼不工作...我也想知道它...添加框架不是正確的答案,並沒有幫助 –

4

可以加載的數據是這樣的:

NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; 

而且你可以實例URL的陣列也以此方式:

NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","]; 

但是,如果有人滾動圍繞在桌子上了很多,隨着細胞的回收,您可能會多次加載圖像。

2

也許你可以試試https://github.com/SpringOx/ALImageView.git。它要簡單得多,比SDWebImage.You只需要兩個源文件(ALImageView.h/ALImageView.m)。您可以重用圖像視圖來重新加載tableview單元格中的不同url。

  1. 支持本地和內存緩存;
  2. 支持佔位符;
  3. 支持點擊觸摸(目標動作);
  4. 圖像視圖的支持角;
+0

我寧願發表評論,然後回答這個內容.. – Mingebag

2

您可以輕鬆地將所有與下面的代碼的幫助的圖像,細節陣列是一個主陣列

Details Array :- { 
     "item_code" = 709; 
     "item_desc" = Qweqweqwe; 
     "item_name" = AQA; 
     "item_photo" = "http://toshaya.com/webapp/snap&sell/api/img_items/709.png"; 
     "item_price" = "0.00"; 
     "item_till" = "20-25"; 
     "item_type" = Orange; 
     latitude = ""; 
     longitude = ""; 
    } 

有了下面的代碼檢索圖片的URL轉換成String

NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""]; 

NSLog(@"%@",result); 

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]]; 
cell.icon.image = [UIImage imageWithData:imageData]; 
的幫助
相關問題