Ricardo P
Ricardo P

Reputation: 13

TableView with asynchronous load remote images

I will explain my problem, I hope to find a solution. I'm looking for weeks how to fix this, but can not find how to do it, do not want to use external libraries or rare classes.

Explanation:

I have a TableView get the information automatically from XML, the XML all the information I have it parsed and stored in a MutableArray.

The array parses all my XML tags, it is working correctly.

To read a label I do my parser as follows:

//To use this tag Title:

[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"name_category_ads"];

//To use this image tag:

[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"image1_category_ads"];

Problem:

The TableView text correctly load my array (Title), but I can not have it load the images from my array in tableview:

I tested my code with a static URL image (an image that is on the website) and asynchronous loading if it works but does not work when I try to do it from my array, that it has different url of the images.

//Example of an image URL Static

NSString *imagenURL=@"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg";

The code for my table is as follows:

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

static NSString *CellIdentifier = @"Cell";

//My custom cell

Cell_Category_Iphone *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[Cell_Category_Iphone alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}

//Image Static URL
NSString *imagenURL=@"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg";

//Assign the title to my cell, using my array already loaded and parsed.

cell.titleCategoryCell.text=[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"name_category_ads"];

//Asynchronous loading of image

if (!cell.imageCategoryCell.image) {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:imagenURL]];

dispatch_sync(dispatch_get_main_queue(), ^{
        UIImage *thumbnail = [UIImage imageWithData:data];
        cell.imageCategoryCell.image=thumbnail;
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    });
});
}

return cell;
}

Rounding out the problem, consisting of:

The code loads the image correctly so asicronica, but a single image that is in a URL, but I can not have it load the images that are in my array.

//My array images

[[self.parseResults objectAtIndex: indexPath.row] objectForKey: @ "image1_category_ads"];

Can you help me? Then share the code for everyone. Many Thanks

regards Ricardo

Upvotes: 1

Views: 2764

Answers (3)

miho
miho

Reputation: 12085

When you are using the AFNetworking library there is a really nice implementation for that. It is a extension to UIImageView which handles the asynchrony loading for you. You just need to set the URL and that's a 1-liner. Have a look at UIImageView+AFNetworking at GitHub.

If you do not want to use AFNetworking library, you may want to have a look at this sample. It is very basic and does the same, but is only one simple class.

Upvotes: 2

Tony
Tony

Reputation: 10208

Here is what you need:

https://github.com/toptierlabs/ImageCacheResize.

It fetches, caches, loads remote images from a URL. Also supports transformations.

Upvotes: 0

Ricardo P
Ricardo P

Reputation: 13

manages to solve the problem. What I did was to verify and eliminate the array of images, white space and line break with that and it works perfectly. regards Ricardo

Upvotes: 0

Related Questions