user3337260
user3337260

Reputation: 39

Xcode tableview images load again and again

- (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;

}

When i scroll up or scroll back images loading again and again i have no lagging when i scroll but i want to load my images into cache so my app does not load my image again.(i want to twitter scrolls)

Upvotes: 3

Views: 124

Answers (1)

Bogdan Somlea
Bogdan Somlea

Reputation: 624

there is a nice category: UIImageView + AFNetworking, wich is dooing everything for you. Just add the AFNetworking pod, and use

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


 [image setImageWithUrl:@"yourURL"];

and this method will take care of everything.

Upvotes: 1

Related Questions