want to extrac image from string url
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
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!
精彩评论