Xcode tableview图像一次又一次加载

时间:2014-07-11 08:35:51

标签: ios objective-c xcode

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

    cell.tag = indexPath.row;
    cell.imageView.image = nil;

    // Rounded Rect for cell image
    CALayer *cellImageLayer = cell.imageView.layer;
    [cellImageLayer setCornerRadius:35];
    [cellImageLayer setMasksToBounds:YES];


    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(queue, ^(void) {


        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_ResimAdi[indexPath.row]]];
        UIImage *image = [[UIImage alloc] initWithData:data];

        if (image) {

            dispatch_async(dispatch_get_main_queue(), ^{
                if (cell.tag == indexPath.row) {

                    CGSize itemSize = CGSizeMake(70, 70);
                    UIGraphicsBeginImageContext(itemSize);
                    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                    [image drawInRect:imageRect];
                    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    [cell setNeedsLayout];

                }
            });
        }
    });

    cell.TitleLabel.text = _OUrArray[indexPath.row];
    return cell;

}

当我向上滚动或向后滚动图像时,一次又一次地加载我没有滞后当我滚动但我想将图像加载到缓存中以便我的应用程序不再加载我的图像。(我想推特滚动)

1 个答案:

答案 0 :(得分:1)

有一个很好的类别:UIImageView + AFNetworking,它正在为你做一切。只需添加AFNetworking pod,然后使用

- (void)setImageWithURL:(NSURL *)url;


 [image setImageWithUrl:@"yourURL"];

并且这种方法将处理所有事情。