views:

88

answers:

1

Hi all,

I'm trying to use the Google API to put some graphics in my application but it's not workig. I tested the code for other images on internet and it worked.

Code:

- (void)viewDidLoad {  
    [super viewDidLoad];  
    UIImage *myimage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://chart.apis.google.com/chart?cht=bvo&chd=t:10,50,60,80,40&chl=Hello|World|hi&chs=250x100"]]];  
    UIImageView *test = [[UIImageView alloc] initWithImage:myimage];  
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];  
    [myView addSubview:test];  
    [self.view addSubview:myView];  
}

Thanks, Claudio

+1  A: 

Part of your problem is that you have compounded so many statements into one line. If you broke it up it would be easier to tell what is going on.

The issue is the '|' character in the URL string. If you give the chart a plain alphanumeric title it will load. If you replace the '|' with "%7C" it will load.

"http://chart.apis.google.com/chart?cht=bvo&chd=t:10,50,60,80,40&chl=Hello%7CWorld%7Chi&chs=250x100"

is the URL you should use.

See this answer for ways you can check every string you want to use as a URL - it's a good idea to check for all problematic characters or things will fail when you least expect it.

Adam Eberbach
Very nice! Thanks a lot!
Claudio