tags:

views:

39

answers:

1

I have the following image xml tag in which image path is stored

NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];

now i want to display this image in UITAble cell by following method

cell.textLabel.text=imgstring;

i am just getting path on the cell not actual image how should i display image

A: 

Hi.

Of course you're getting text displayed - you're setting the text property of a UILabel to a string :) How would it know you wanted it to display an image?

You want something like

cell.imageView.image = [UIImage imageNamed:imgstring];

Have a look at the UITableViewCell documentation for more details

However, this will only work for images in your project - for external images you will have to do a little more work.

NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];

However, this will block your app until the image is loaded. Instead you might need to load the images is the background :

    // Tell your code to load the image for a cell in the background
    NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell, @"cell", imgstring, @"imgstring", nil];
    [self performSelectorInBackground:@selector(loadImage:) withObject:dict];

- (void) loadImage:(id)object {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Get the data to pass in
    NSDictionary *dict = (NSDictionary *)object;

    // Get the cell and the image string
    NSString *imgstring = [dict objectForKey:@"imgstring"];
    UITableViewCell *cell = [dict objectForKey:@"cell"];

    NSURL *url = [NSURL URLWithString:imgstring];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];

    [pool release];
}

But you're going to run into threading problems if you're not careful with this :)

If you get any other problems, post another comment and I'll try to help!

PS I've not tested this code, just typed it straight in so there might be typos, sorry!

deanWombourne
Dear deanWombourne thanks for replyingactually my image is comming from url which is in xml tag named "image"How should i extract this image from url and show this image in table cell
Nauman.Khattak
Hi, I've edited my answer, hope it's more helpful :)
deanWombourne
Thanks it works.........
Nauman.Khattak
hi DeanI am failing to implement the second method which you send to me. The first method is working but as u warn at first that it will block your application exactly it is so. Kindly tell me how to implement second method i.e load image in background.
Nauman.Khattak
the problem is that it load images but not with exact title i mean that for tom cruise it has creae thumbnil of janifer lopez and so on
Nauman.Khattak