iPhone开发 - 来自Google API的图表

Cla*_*dio 1 iphone api charts

我正在尝试使用Google API在我的应用程序中放置一些图形,但它不适用.我在互联网上测试了其他图像的代码并且有效.

码:

- (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];  
}
Run Code Online (Sandbox Code Playgroud)

谢谢,克劳迪奥

Ada*_*ach 7

你的部分问题是你将这么多的陈述复合到一行中.如果你把它搞砸了,就会更容易分辨出发生了什么.

问题是'|' URL字符串中的字符.如果您为图表提供一个简单的字母数字标题,它将加载.如果你替换'|' 加载"%7C"将加载.

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

是您应该使用的URL.

请参阅此答案,了解您可以检查要用作URL的每个字符串的方法 - 检查所有有问题的字符是个好主意,或者在您最不期望的情况下事情会失败.