2012-10-09 64 views
3

我使用的tableview從文檔目錄加載圖像,創建縮略圖並在tableview中顯示它。但是,我遇到了一個問題:由於照片很大,使用相機拍攝時會變得緩慢並且崩潰。使用SDWebImage將本地圖像從文檔目錄加載到tableview

我已經探索了幾種解決方案,包括GCD做的工作在後臺線程,但結果是一樣的東西。所以,我想看看SDWebImage,但我不知道它是否也適用於本地文件,而不是這種情況下的網絡圖像。有人可以提醒我嗎?如果不是,這個問題如何解決?有沒有可以幫助解決這個問題的API?

回答

0

這個問題不容易回答,因爲這個問題相當廣泛,但我會盡我所能。

首先,我通常派遣一個後臺線程,如果我有昂貴的處理做的,以不阻塞主線程,這是相當重要的。 我真的不知道你爲什麼不使用正常的UIImageView爲你在做什麼,但試圖實現以下幾點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"YourCell"; 
    MyCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[MyCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
/* 
Whatever Code you want 
*/ 
    NSArray* params [email protected][cell.myImageView, @"http://myfancyimages.com/image.png"]; 
    [self performSelectorInBackground:@selector(loadEventImageWithParameters:) withObject:params]; 
    return cell; 
} 

現在添加功能:

- (void) loadEventImageWithParameters:(id) parameters { 
    NSArray* params = [[NSArray alloc] initWithArray:(NSArray*)parameters]; 
    NSURL *url = [NSURL URLWithString:[params objectAtIndex:0]]; 
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
    UIImageView* theImageView = (UIImageView*) [params objectAtIndex:0]; 
    [theImageView setImage:image]; 
} 

如果你有一個很多圖片加載你,建議排隊你的進程,所以你不要「竊取」大中央調度的所有資源。 請仔細閱讀此優秀帖子http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial以瞭解更多詳情。

希望能幫到

+0

感謝您的回答。是的,我正在設計數百個大型文件,並且我最終實施了GCD解決方案,並取得了積極成果。 –

相關問題