2012-03-27 41 views
0

幾乎每個ios應用程序現在都有類似「Feed」選項的內容。 編程通常包括從網絡獲取圖像,緩存它們,處理頁面,「拉動更新選項」等 - 所有非標準的東西。帶有圖像緩存和「拉動更新」選項的表格視圖

但看起來像沒有標準的解決方案呢?

  • 我試過「三個20」 - 真的很大,很多模塊複雜的庫。它真的缺乏良好的文檔!從緩存中提取圖像時,它也會「減速」。

  • 也許我應該爲每個小任務分別使用不同的小型圖書館?像HJCache,EGO等

  • 或者是從零開始編寫所有的東西而不使用任何庫嗎?

請給我這裏的最佳實踐的建議,我現在真的卡住了。

回答

0

This one很容易放入拉刷新。

對於圖像加載,我寫了下面類別的UIImageView:

// .h 
@interface UIImageView (UIImageView_Load) 
- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion; 
@end 

// .m 
#import "UIImageView+Load.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation UIImageView (UIImageView_Load) 

- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion { 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     if (data) { 
      self.image = [UIImage imageWithData:data]; 
      if (completion) completion(self.image); 
     } 
    }]; 
} 

@end 

// To use it when building a cell 

//... 

MyModelObject *myModelObject = [self.myModel objectAtIndex:indexPath.row]; 
if (myModelObject.image) { 
    cell.imageView.image = myModelObject.image; 
} else { 
    NSURL *url = [NSURL urlWithString:myModelObject.imageUrl]; 
    [cell.imageView loadFrom:url completion:^(UIImage *image) { 
     // cache it in the model 
     myModelObject.image = image; 
     cell.imageView.image = image; 
    }]; 
} 

// ... 
+0

感謝您的回答@danh!不幸的是,它不包含任何緩存解決方案。但是我會給lea的「拉來刷新」一試。 – Danik 2012-03-28 11:14:43

+0

@Danik - 當然。我粘貼的代碼在回調模型中進行了延遲加載和緩存。 – danh 2012-03-28 13:34:11

+0

是的,我現在看到,您將其緩存在myModelObject.image中!您是否清除了didReceiveMemoryWarning上的緩存或限制它的最大尺寸? – Danik 2012-03-28 18:30:33

0

我利·卡爾弗的Pull to Refresh圖書館的粉絲,或者這STableViewController它處理拉至刷新以及無盡的滾動向下。

對於圖像加載,請嘗試從DailyMotion應用程序SDWebImage

+0

謝謝!我還使用Leah的Pull to Refresh和SDWebImage來處理當前的項目。 – Danik 2012-07-10 08:32:29