Load image from server on a UIImageView in phone - iphone

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];
}

Related

Cannot Fetch UIImage from URL

i am trying to fetch an image from URL, but i am not able to do it.
can any one let me know what went wrong
find the code below for your reference.
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
img.image = [UIImage imageWithData:imageData];
where img is a UIImageView
i just create DEMO of Your ImageURL you just Need to put Code Bellow:-
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
ImageURL =[ImageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#"img url ==%#",ImageURL);
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *MYimage = [UIImage imageWithData:data];
NSLog(#"==%#",MYimage);
[imgview setImage:MYimage];
}
Download the Demo i just created of your image URL:-
http://www.sendspace.com/file/m966ae
Thank you :)
Try this.......
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *IMG = [UIImage imageWithData:data];
img.image = IMG;
This is because you have "{" and "}" in your imageURL so you are not able to get your image, so just replace this braces.
-(NSString*)replace:(NSString*)string
{
NSString *str = [[string stringByReplacingOccurrencesOfString:#"{" withString:#"%7B"] stringByReplacingOccurrencesOfString:#"}" withString:#"%7D"];
return str;
}
You have to create your url object in the following way :
NSURL *url = [NSURL URLWithString:[ImageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
then use that url to fetch data. Hope it will work for you.
there are multiple ways to get Image from Url. you can use anyone. but the fastest one that i have tried is
-(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr
{
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}

Not able to get images from a link to imageview

In my Iphone app.i'm not getting the images in imageview.Those images are coming from a link.
link is http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg
i'm using this code.
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Try this :-
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Hope it helps you
try this code...
NSURL *imgURL = [NSURL URLWithString:#"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgURL];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
yourImageView.image = tmpImage;
You should make sure the [NSData dataWithContentsOfURL: [NSURL URLWithString:str]] returns not nil . if nil you should use dataWithContentsOfURL:options:error:.
quote Apple Documentation :
Return Value
A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.
Discussion
If you need to know what was the reason for failure, use dataWithContentsOfURL:options:error:.
I've tried the following and it is working fine for me:
NSString *str = #"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg";
//NSLog(#"Image data %#", [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]);
UIImageView *imgv = [[[UIImageView alloc] initWithFrame: CGRectMake(50.0, 50.0, 100.0, 100.0)] autorelease];
imgv.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
imgv.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgv];

help needed with adding picture from url!

i've managed to incorporate the twitter api into my app and i can call all the data and put it into my app, except for the image.
i've managed to find tuts on how to get the picture if you provide the url, but i want to get the addy from my dictionary to then be put into my uiimageview.
Any help would be appreciated, here is what i have sourced
NSData *imageData =
[[NSData alloc] initWithContentsOfUrl:
[NSString
stringWithContentsOfUrl:
[NSURL URLWithString:#"http://mydomain.com"]
encoding: NSUTF8StringEncoding
error: nil
]
];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[marcaBackground setImage:image];
[imageData release];
[image release];
this is what i have already set up`-(NSString*)author {
return [[contents objectForKey:#"user"] objectForKey:#"screen_name"];
`
NSString *author=[(Tweet*)[auth objectAtIndex:indexPath.row] author];
could someone please advise me on what needs changing for it to work??
NSData *imageData = [[NSData alloc] initWithContentsOfUrl: [NSURL URLWithString:#"http://..."]];
The rest is correct.

How do you set a UIImage using an absolute URL?

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];

NSURL code to display an image from a sqlite table

Ii'm using this NSURL code to display an image. what I want to do is to insert a url from my database table in place of the static url shown in the code below:
Does anyone know how I should proceed?
NSURL *url = [NSURL URLWithString:#"http://www.nyapplecountry.com/images/photosvarieties/redrome04.jpg"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
[self.containerView addSubview:self.mainView];
I was thinking of something like:
NSString *urlAddress = [NSString stringWithFormat:#"%#", place.url];
where place.url is coming from my db table. I'm missing something though, not sure what.
thanks for any help
Is place.url a NSString object and the full URL to the image? Then it would be as simple as
NSURL *url = [NSURL URLWithString:place.url];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
You can't do it that way. You have to read image from data base and initialize UIImage with that data.