簡體   English   中英

使用SDWebImage將文檔目錄中的本地映像加載到tableview

[英]Loading local images from documents directory to tableview using SDWebImage

我正在使用tableview從文檔目錄加載圖像,創建縮略圖並在tableview中顯示它。 但是,我有一個問題:它變得很慢並且因為照片很大而使用相機拍攝時崩潰。

我已經探索了幾個解決方案,包括GCD在后台線程中完成工作,但結果是一樣的。 所以,我想調查SDWebImage,但我不知道它是否也適用於本地文件,在這種情況下不適用於Web圖像。 有人可以告訴我嗎? 如果沒有,這個問題怎么解決了? 是否有可以幫助解決此問題的API?

這個問題不容易回答,因為問題被廣泛提出但我會盡我所能。

首先,我通常會派遣一個后台線程,如果我有昂貴的處理要做,以免阻塞主線程,這是非常重要的。 我真的不知道為什么你沒有使用正常的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 =@[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];
}

如果你有很多圖片需要加載,你最好排隊你的進程,這樣你就不會用Grand Central Dispatch“竊取”所有資源。 請閱讀這篇優秀的文章http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial了解更多詳情。

希望有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM