2014-04-22 28 views
2

我有一個可用視圖,包含兩個UImage的自定義單元格。標誌圖像取自一個在線網站,這就是爲什麼需要緩存圖像。加載圖像到現在爲止是這樣做:使用URL中的uitableview緩存UIImage

NSURL * imageURL = [NSURL URLWithString:[arra1 objectAtIndex:indexPath.row/2]]; 
NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 

NSURL * imageURL2 = [NSURL URLWithString:[arra2 objectAtIndex:indexPath.row/2]]; 
NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2]; 

cell.ima1.image = [UIImage imageWithData:imageData]; 
cell.ima2.image2 = [UIImage imageWithData:imageData2]; 

我從搜索得知,是dataWithContentsOfURL不是異步的,滾動時會佔用大量的時間。我嘗試了幾種方法,但我似乎無法得到正確的一個。這是我第一次緩存UIImages,我將非常感謝有關實施的詳細解釋,所以我可以學習完成工作。從蘋果

  • MonoTouch-LazyTableImages
  • robertmryan- LazyTableImages應用示例 - - 非常感謝

  • +3

    到緩存的UIImage使用SDWebImage https://github.com/ rs/SDWebImage –

    +0

    @JayGajjar謝謝你的回覆,我試過但我迷路了,我真的很新,你能告訴我一個實現嗎? –

    +0

    其簡單隻需使用setImageWithURL與你的uiimageview。其餘的將由sdk管理。 –

    回答

    3

    我使用這個庫這僅僅是完美

    你只需要#import <SDWebImage/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://www.domain.com/path/to/image.jpg"] 
            placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
    
        cell.textLabel.text = @"My Text"; 
        return cell; 
    } 
    

    它還緩存下載的圖像,併爲您提供卓越的性能。

    希望它能幫助你!

    +1

    我無法謝謝你!在我使用SDWebImage之前,但是我沒有正確地實現它,所以結果很麻煩..如果使用placeHolderImage,這個方法的祕密!永遠不要刪除它,這就是我一直在嘗試的東西和@Erid建議的內容之間的區別......謝謝,這對所有未來的開發者來說都是考慮到的,... –

    +0

    我很高興它幫助你! :) – EridB

    0

    您可以檢查這些示例應用程序

    1. LazyTableImages從蘋果的示例應用程序清楚地解釋了限制。

    希望這會有所幫助。

    1

    SDWebImage,在我看來,是最好的選擇。

    您只需將其包含在您的應用程序,並使用它像這樣:

    • SDWebImageManager *manager = [SDWebImageManager sharedManager]; 
      
      [manager downloadWithURL:[NSURL URLWithString:image_url] 
             options:0 
             progress:nil 
             completed:^(UIImage *images, NSError *error, SDImageCacheType cacheType, BOOL complete) { 
      
              myImageView.image = images; 
             }] ; 
      

    它下載圖像異步,所以它不會阻塞UI。

    +0

    使用它仍然阻止了tableview,仍然需要太長時間滾動:S我錯過了什麼 –

    +0

    你在哪裏添加了這段代碼? – n00bProgrammer

    +0

    cellForRowAtIndexPath我應該把它添加到別的地方嗎? –

    0

    結帳UIImageLoader https://github.com/gngrwzrd/UIImageLoader

    容易加載圖像,你得到的回調,你會要處理的情況:

    NSURL * imageURL = myURL; 
    
    [[UIImageLoader defaultLoader] loadImageWithURL:imageURL \ 
    
    hasCache:^(UIImage *image, UIImageLoadSource loadedFromSource) { 
    
        //there was a cached image available. use that. 
        self.imageView.image = image; 
    
    } sendRequest:^(BOOL didHaveCachedImage) { 
    
        //a request is being made for the image. 
    
        if(!didHaveCachedImage) { 
         //there was not a cached image available, set a placeholder or do nothing. 
    
         self.loader.hidden = FALSE; 
         [self.loader startAnimating]; 
    
         self.imageView.image = [UIImage imageNamed:@"placeholder"]; 
        } 
    
    } requestCompleted:^(NSError *error, UIImage *image, UIImageLoadSource loadedFromSource) { 
    
        //network request finished. 
    
        [self.loader stopAnimating]; 
        self.loader.hidden = TRUE; 
    
        if(loadedFromSource == UIImageLoadSourceNetworkToDisk) { 
         //the image was downloaded and saved to disk. 
         //since it was downloaded it has been updated since 
         //last cached version, or is brand new 
    
         self.imageView.image = image; 
        } 
    }];