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!
Related
I am trying to create an array of images (from the web) and then pass this array to a new view. Here is the code I'm using:
WebViewController *webController = segue.destinationViewController;
NSURL *url = [NSURL URLWithString:[gift objectForKey:#"smallImageUrl"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
webController.image = image;
Also in my WebViewController.h i have:
#property(strong,nonatomic) UIImage *image;
But when I try to use "image" in WebViewController.m all i get is a (null) value. Where am I going wrong? Thanks!
UPDATE:
The problem appears to not be as I stated before. The image makes it to WebViewController. I am trying to save the image to an array which must be where my problem lies. This is the code I have for that in a saving method (in WebViewController.m):
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:image];
[imageArray writeToFile:imageFileName atomically:YES];
also in my viewdidload i do:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
imageFileName = [NSString stringWithFormat:#"%#/imageArray",documentsDirectory];
hi this is a part of my code
NSURL *jsonURL = [NSURL URLWithString:#"http://*****.php"];
NSString *donneeJson = [[NSString alloc] initWithContentsOfURL:jsonURL];
self.pseudoOnline = [donneeJson JSONValue];
NSDictionary *json2 = [pseudoOnline valueForKey: #"photo"];
NSLog(#"adresse photo%#",[pseudoOnline valueForKey: #"photo"]);
NSString *url = [[pseudoOnline valueForKey: #"photo"]objectAtIndex:0];
NSLog(#"adresse photo%#",url );
adresse photo(
"http://************/XWsmG18O27.jpg",
"http://************/Wz0ab6oIxU.jpg",
"http://***********/9yKzQrpO59.jpg",
"http://************/l2rbNeIK8a.jpg")
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
how i can download all the pictures because iam only take picture index0 !
thks
thks
ads2 is an instance of NSArray, not NSDictionary. NSArray is not a key-value collection and does not respond to valueForKey:
I'm having a problem loading a remote image into a UIImageVIew...
It just doesn't show the image, may be i'm missing something...
I also use the described here but with the same results
How to load image from remote server on the UIImageView in iphone?
Can someone help me?
This is the code i'm using
Im getting the data from a xml and on the image element I have the full path
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#", [[promoList objectAtIndex: promoIndex] objectForKey: #"image"]] ]]] ];
With this code the image are displayed correctly
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: #"http://localhost/promos/preview/1.jpeg"]] ]];
Since the bottom example works as expected and besides the url string the example looks the same the problem should be with getting the url string. The problem is also that the code looks the same but it is quite hard to see. I would refactor the code to something like:
NSObject *urlObject = [[promoList objectAtIndex:promoIndex] objectForKey:#"image"];
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
[[detailViewController detailImage] setImage:image];
Then you can debug the statement properly and make sure that you get what you expect after each step.
Update
To remove newline and tabs you can:
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
If urlObject already is a string, which it should be, then you can do:
NSString *urlString = [urlObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
which would make it a bit cleaner.
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:#"http://...."]]] retain];
if (img != nil)
{
[imageViewName setImage:img];
[img release];
}
I am trying to set the UIImage of a tableview cell to an absolute url, here's the code i have already:
NSString * imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"images"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//set image
cell.imageView.image = img;
But for some reason this returns null as if the URL supplied is invalid. However i know this is not the case because i can make it work by typing one of the URLs as a string literal into the imageURL pointer and i know the code [[stories objectAtIndex:indexPath.row] objectForKey:#"images"] returns correct URLs as i printed the output to the log and it was fine.
Is there any reason why this would be happening that I'm clearly not seeing here?
You can use the absoluteURL method of NSURL and voila!
NSURL *url = [[NSURL URLWithString:imageURL] absoluteURL];
in my iphone application images are comming from xml feed and they have variant size,how can i resize them to fix width and hight programmatically.
my code is in which i want to display images in cell of same width and size.my image are comming from rss fedd.
int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image=img;
please help me out if you can thanks in advance.
Hi change your code like this,
NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 10.0f,60.0f, 60.0f)];
[subview setImage:[UIImage imageNamed:img]];
[subview setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubView:subview];
try this one. I hope it will help you.