Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 733
user3337260 Avatar asked Mar 23 '26 14:03

user3337260


1 Answers

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.

like image 160
Bogdan Somlea Avatar answered Mar 25 '26 04:03

Bogdan Somlea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!