keep on you
keep on you

Reputation: 310

How can i store photos if they are downloaded?

I have a UITableViewCell that contains 4 photos and i get these photos from the web but the problem is when i scroll down the UITableView these photos are downloaded again enter image description here

And this is the code:

       ITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellPhoto"];

if (cell == nil) {

    NSArray *nibObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCellThumbnails" owner:self options:nil];
    cell = [nibObject objectAtIndex:0];
}

// Get the photos
int getPhotos = indexPath.row * 4;

for (int i = 1; i <= 4; i++) {

    if (getPhotos < [imageArray count]) 
    {
        UIButton *imageButton = (UIButton*)[cell viewWithTag:i];
        NSString *url = [imageArray objectAtIndex:getPhotos];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.siteweb.com%@",url]]];
            UIImage *imageFieldProfile = [[UIImage alloc] initWithData:imageData];
            dispatch_async(dispatch_get_main_queue(), ^{
                // Set the phoyo to the UIButton
                [imageButton setBackgroundImage:imageFieldProfile forState:UIControlStateNormal];
                [imageFieldProfile release];
                // Set the corner of UIButton
                [imageButton.layer setCornerRadius:5.0];
                [imageButton.layer setMasksToBounds:YES];
                imageButton.tag = getPhotos;
            });
        }); 

        [imageButton addTarget:self action:@selector(displayPhoto:) forControlEvents:UIControlEventTouchUpInside];

    }         
    getPhotos ++;
}

Upvotes: 0

Views: 83

Answers (3)

Till
Till

Reputation: 27597

You could use a combination of lazy loading image views in combination with local caching. This would be rather easy to accomplish using ASIHTTPRequest in combination with correctly setup caching flags.

ASIHTTPRequest is extremely easy to use and its caching is very well controllable.

In contrast to the other solutions suggested, I would stick to use UITableView and its UITableViewCells as this will allow you to use de/queued cells without having to build such logic yourself.

I have used such solution for a major newsmagazine app (over 2mio downloads) and am totally satisfied with the results.

Upvotes: 0

hotpaw2
hotpaw2

Reputation: 70743

You should save or cache the images in another object when they are downloaded, not in the table view cell. Perhaps using some sort of data source or model object from which the table view cell requests the images, instead of having the table view cell directly make any URL requests. Then the model object can cache images after downloading and before handing them to the cell.

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

You should use the view controller to fill the cells, not the UITableViewCell. If you do that, it's not only a better coding style, it's also easier to save the data.

Anyway, if you really must: initialize the UITableViewCell with some kind of storage table, so that you can store the data you downloaded: rewrite initWithStyle:reuseIdentifier: to initWithStyle:reuseIdentifier:usingCacheTable:

But, again, the correct way to do this is to load the data in the view controller and let the UIView subclasses simply only show stuff.

Upvotes: 1

Related Questions