2010-08-06 72 views
1

我正在嘗試使用Google API在我的應用程序中放置一些圖形,但它並不工作。我在互聯網上測試了其他圖像的代碼,它工作。iPhone開發 - 來自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]; 
} 

感謝, 克勞迪奧

回答

7

您的問題之一是,你加重了這麼多的語句插入一行。如果你把它分解出來,會更容易分辨出發生了什麼。

問題是'|'字符在URL字符串中。如果你給圖表一個簡單的字母數字標題,它會加載。如果您替換「|」與「%7C」它將加載。

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

是您應該使用的URL。

請參閱this answer瞭解如何檢查要用作URL的每個字符串 - 最好檢查所有有問題的字符,否則在最不經意的時候會失敗。

+0

非常好!非常感謝! – Claudio 2010-08-06 05:59:04