从iPhone应用程序中的URL下载图像

时间:2010-08-12 11:57:55

标签: iphone objective-c uiimageview uiimage

我在iPhone应用程序中使用谷歌图表功能....我想下载图像并将其从http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World放入UIImageView ... 是否有用于执行此类功能的示例应用程序?或以任何其他方式在应用程序中显示图表?

提前致谢...

3 个答案:

答案 0 :(得分:2)

这会阻止,使用NSURLConnection进行非阻塞下载

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];

myImageView.image = downloadedImage;

答案 1 :(得分:0)

尝试imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]]

答案 2 :(得分:0)

@iSwap - 我知道为什么你的代码无效。

我认为你把一个url作为字符串而不是url。 如上面的代码----

imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];

有一个urUR应该是一个url而不是一个字符串,你把它写成一个像

这样的字符串
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];

您忘记将此字符串转换为网址。

正确的方法将是这样的 -

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

我希望你能得到它,而且“willcodejavaforfood”和“CodeBrickie”代码之间没有区别。只是写作方式略有不同。