2

I have a URL with tag:

http://ws.cdyne.com/WeatherWS/Images/sunny.gif

When I do like this:

NSURL *url=[NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/sunny.gif"];

it's getting image in gdb and I am able to download from this URL. But when I write code like this:

NSURL *url=[jsonItem objectForKey:@"PictureURL"];  

(where jsonItem is dictionary and passing it to data and then to UIImage), then it shows an exception.

When I get the value of:

  [jsonItem objectForKey:@"PictureURL"]; 

in gdb then it shows like:

2011-03-15 11:08:16.405 XML[1576:20b] jsonURKL  ..... ...

http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif   (in next line in gdb)

I need to download through this:

NSURL *url=[jsonItem objectForKey:@"PictureURL"];

I am not able to do that. What could be wrong?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Ketan Shinde
  • 1,847
  • 4
  • 18
  • 38
  • Can you post the code that creates the `jsonItem` dictionary? If not, can you `NSLog` the value of `[jsonItem objectForKey:@"PictureURL"]`? We need some more clues as to what class it is, etc. – Tim Mar 15 '11 at 06:02
  • 2011-03-15 11:34:28.383 XML[1709:20b] value ..... ... http://ws.cdyne.com/WeatherWS/Images/partlycloudy.gif – Ketan Shinde Mar 15 '11 at 06:06
  • but i have doubt that the value of url that i am geting in gdb is in next line... – Ketan Shinde Mar 15 '11 at 06:11

2 Answers2

2

Url itself one string. so you have to use below code

NSURL *url=[NSURL URLWithString:[jsonItem objectForKey:@"PictureURL"]];
NSData *data = [NSData dataWithContentsOfURL: url];

now setimgge with [UIImage imageWithData: data]; this code

GameLoading
  • 6,688
  • 2
  • 33
  • 57
  • the value showing null value!! i guess !! the problem is of space or next line that contain in my xml tag,,, – Ketan Shinde Mar 15 '11 at 06:41
  • 2011-03-15 12:20:33.597 XML[2068:20b] jsonItem....{ PictureURL = "\n http://ws.cdyne.com/WeatherWS/Images/sunny.gif"; } – Ketan Shinde Mar 15 '11 at 06:49
  • i have doubt on "\n".. might be i am not getting the value in nsdata. – Ketan Shinde Mar 15 '11 at 06:51
  • this xml file is not on my local host i am fetching it from web! – Ketan Shinde Mar 15 '11 at 06:54
  • 2
    nsstring *tempstr=[jsonItem objectForKey:@"PictureURL"]; tempstr=[tempstr stringByReplacingOccurrencesOfString:@"\n" withString:@"Http://"]; now u can edit this string – GameLoading Mar 15 '11 at 06:57
  • thanks!! now its working!! i have done like this: NSString *tempstr=[jsonItem objectForKey:@"PictureURL"]; tempstr=[tempstr stringByReplacingOccurrencesOfString:@"\n h" withString:@"h"]; – Ketan Shinde Mar 15 '11 at 07:08
1

please do this in following way,

NSURL *url=[NSURL URLWithString:[jsonItem objectForKey:@"PictureURL"]];
NSData *data = [NSData dataWithContentsOfURL: url];
cell.iconImage.image=[UIImage imageWithData: data];
Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
  • 1
    [jsonItem objectForKey:@"PictureURL"] contain only url, so i cannot use NSURL *url=[NSURL URLWithString:[jsonItem objectForKey:@"PictureURL"]]; instead i am using NSURL *url=[jsonItem objectForKey:@"PictureURL"]; – Ketan Shinde Mar 15 '11 at 06:13
  • its not the problem!! i guess!! but the problem is when i get the value of [jsonItem objectForKey:@"PictureURL"]; then it shows the url in next line.. is it matter or any kind of issue ?? – Ketan Shinde Mar 15 '11 at 06:14
  • i think..this is the right way...otherwise its a problem of yar parsing or on yar server side...if u trace it... let me know.. – makboney Mar 15 '11 at 06:36
  • does any body know how to get url without spaces or any other character in nsurl? – Ketan Shinde Mar 15 '11 at 06:43