【问题标题】:NSURL and NSURLConnection do not work with Google Chart APINSURL 和 NSURLConnection 不适用于 Google Chart API
【发布时间】:2011-04-20 15:14:11
【问题描述】:

我尝试显示由 Google Chart API 返回的图像,但以下代码不起作用:

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
imgView.image = downloadedImage;

NSData *imageData=[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]] returningResponse:nil error:nil];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
imgView.image = downloadedImage;

目标图像未按预期显示。你知道问题出在哪里吗?

【问题讨论】:

    标签: api nsurlconnection charts nsurl


    【解决方案1】:

    除了NSURL 对象为nil 之外,这两条代码都有效。 NSURL 不支持管道字符 (|),因此您需要使用 %7c 对其进行转义。您可以使用[NSString stringByAddingPercentEscapesUsingEncoding:] 来处理任何其他字符。这是新版本:

    NSString *url = [@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *downloadedImage = [UIImage imageWithData:imageData];
    imgView.image = downloadedImage;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多