优化技巧一、UITableView加载图片

我的想法是TableView滚动的时候不去加载未加载过的图片,停止滚动后再从网络加载。已经加载过得图片,无论什么时候都加载该图片(因为SDWebImage会将加载过得图片缓存下来,再次加载的时候从缓存中取,这样就不用开辟线程下载图片了)

#pragma mark - UITableView Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    if((self.tableView.isDragging || self.tableView.isDecelerating) && ![self.cellArray containsObject:indexPath]) {
            [cell.testImageV sd_setImageWithURL:[NSURL URLWithString:self.datasArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"icon_abc"]];
            // 滚动时取消任务的下载
            [cell.testImageV sd_cancelCurrentImageLoad];
        return cell;
    }
    
    [cell.testImageV sd_setImageWithURL:[NSURL URLWithString:self.datasArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"icon_abc"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (![self.cellArray containsObject:indexPath]) {
            [self.cellArray addObject:indexPath];
        }
        if (image && cacheType == SDImageCacheTypeNone) {
            CATransition *transition = [CATransition animation];
            transition.type = kCATransitionFade;
            transition.duration = 0.3f;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            // weakself.layer
            [cell.testImageV.layer addAnimation:transition forKey:nil];
        }
        
    }];
    return cell;
}

#pragma mark - UITableViewDelegate Methods 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        [self.tableView reloadData];
    }];
    [[SDImageCache sharedImageCache] clearMemory];

}

#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    // 慢滑结束触发
    if (! decelerate) {
        [self reLoad];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 慢滑结束触发
    [self reLoad];
    
}

#pragma mark - Private Methods
- (void) reLoad {
    // 刷新当前可见的cells
    NSArray *arry = [self.tableView indexPathsForVisibleRows];
    NSMutableArray *arrToReload = [NSMutableArray array];
    for (NSIndexPath *indexPath in arry) {
        if (! [self.cellArray containsObject:indexPath]) {
            [arrToReload addObject:indexPath];
        }
    }
    // UITableViewRowAnimationFade
    [self.tableView reloadRowsAtIndexPaths:arrToReload withRowAnimation:(UITableViewRowAnimationNone)];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,566评论 4 61
  • 系列文章: TableView优化之高度缓存功能 TableView优化之加载图片的优化逻辑 TableView优...
    老司机Wicky阅读 12,410评论 30 85
  • 之前加入了一个时间管理小组,目标是坚持在30天,每天做一个事情。当时我确定一个目标,每天写一篇没有主题的文章。 这...
    何贰萌阅读 1,661评论 0 0
  • 相遇了,就好好珍惜,谁也预料不到明天是否还能有机会给彼此一个关心,几百年的轮回换来今生的擦肩而过,一个缘字,包含了...
    山东马哥阅读 1,301评论 0 1

友情链接更多精彩内容