SDWebImage(內(nèi)部實(shí)現(xiàn)原理)

(1)涉及知識(shí)點(diǎn)

     01 字典轉(zhuǎn)模型
     02 存儲(chǔ)數(shù)據(jù)到沙盒,從沙盒中加載數(shù)據(jù)
     03 占位圖片的設(shè)置(cell的刷新問題)
     04 如何進(jìn)行內(nèi)存緩存(使用NSDictionary)
     **注意**:使用一個(gè)字典,可以臨時(shí)保存數(shù)據(jù),但是會(huì)出現(xiàn)內(nèi)存問題(內(nèi)存警告),如果不是SDWebImage,我們需要手動(dòng)清除內(nèi)存;
     05 在程序開發(fā)過程中的一些容錯(cuò)處理
     //注意:在圖片還在下載過程中,系統(tǒng)又重新下載,我們需要定義一個(gè)操作字典,來保存我們已經(jīng)加入的操作,避免反復(fù)操作!
     06 如何刷新tableView的指定行(解決數(shù)據(jù)錯(cuò)亂問題)
     //注意:在cell還沒有加載圖片完成的時(shí)候就已經(jīng)被循環(huán)使用到下面去了會(huì)導(dǎo)致我們的cell出現(xiàn)數(shù)據(jù)錯(cuò)亂的問題,所以我們使用指定行刷星,避免出現(xiàn)這樣的問題;
     07 NSOperation以及線程間通信相關(guān)知識(shí)
Snip20151016_1.png
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *path = NSTemporaryDirectory();
    NSLog(@"%@",path);
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    LPJModel *model = self.models[indexPath.row];
    cell.detailTextLabel.text = model.download;
    cell.textLabel.text = model.name;
    //cell.image
    //先從字典中找
    UIImage *cacheImage = self.cacheImages[model.icon];
    if (!cacheImage) {
        //如果沒有就從沙盒中找
        NSData *data = [NSData dataWithContentsOfFile:[model.icon cacheDir]];
        UIImage *image = [UIImage imageWithData:data];
        image = nil;
        if (image) {
            cell.imageView.image = image;
        }else
        {
            //添加占位圖
            cell.imageView.image = [UIImage imageNamed:@"1"];
            //看看是否存在下載操作
            NSOperation *downloadOp = self.operations[model.icon];
            if (!downloadOp) {
                NSOperationQueue *queue = [[NSOperationQueue alloc]init];
                NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
                    NSURL *url = [NSURL URLWithString:model.icon];
                    NSData *data1 = [NSData dataWithContentsOfURL:url];
                    UIImage *image1 = [UIImage imageWithData:data1];
                    [self.operations removeObjectForKey:model.icon];
                    //                cell.imageView.image = image1;
                    //刷新一行
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:NO];
                        //將操作從字典中移除
                        
                    }];
                    [self.cacheImages setValue:image1 forKey:model.icon];
                    //將圖片存在沙盒中
                    [data1 writeToFile:[model.icon cacheDir] atomically:YES];
                }];
                [queue addOperation:op];
                //將操作加入字典
                [self.operations setObject:op forKey:model.icon];
//                NSLog(@"%@",self.operations);
                }else
                    {
                    }
            
            }
        }else
            {
        cell.imageView.image = cacheImage;
    }
    
    return cell;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容