从 url 加载 NSImage 但只有部分 url 有效
我有一些代码可以从网络上加载图像文件并将其放入图像视图中。问题是它适用于除 Google 图表之外的所有内容。这很令人沮丧,因为我依靠它来为我的应用程序绘制数据图表。这是我需要加载的网址: 点击查看我的测试图表。我不确定为什么 NSImage 在与其他所有内容一起使用时似乎拒绝加载它。如果您知道原因或有解决方法,我们将不胜感激。
这是我发现我用来加载图像的一些示例代码:(
NSURL *imageURL = [NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chs=700x400&chd=t:20,20,20,20,20&chl=A|B|C|D|E&chco=66FF33,3333CC"];
NSLog(@"url");
NSData *imageData = [imageURL resourceDataUsingCache:NO];
NSLog(@"data");
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData];
注意:此代码将加载除图表之外的任何图像)
谢谢
I have some code that loads an image file off the web and puts it in an image view. The problem is it works with everything except Google Charts. This is frustrating because I was relying on this to graph data for my app. Heres the url I need to load: Click to see my test chart. Im not sure why NSImage seems to refuse to load this when it works with everything elese. If you know why or have a work-around any help is appreciated.
Here's some sample code I found that I'm using to load the images:
NSURL *imageURL = [NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chs=700x400&chd=t:20,20,20,20,20&chl=A|B|C|D|E&chco=66FF33,3333CC"];
NSLog(@"url");
NSData *imageData = [imageURL resourceDataUsingCache:NO];
NSLog(@"data");
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData];
(Note: This code will load any image except a chart)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是失败的,因为 |不是有效的 URL 字符。您必须替换 | URL 中的字符带有转义码: %7C 。以下网址有效:
http://chart.apis.google.com/chart?cht=p3&chs=700x400&chd=t:20, 20,20,20,20&chl=A%7CB%7CC%7CD%7CE&chco=66FF33,3333CC
This is failing because | is not a valid characted for URLs. You must replace the | characters in your URL with the escape code: %7C . The following URL will work:
http://chart.apis.google.com/chart?cht=p3&chs=700x400&chd=t:20,20,20,20,20&chl=A%7CB%7CC%7CD%7CE&chco=66FF33,3333CC
或者只是使用
那么您不必对所有各种转义进行硬编码。
查看说明
彼得值得竖起大拇指。
Or just use
Then you don't have to hard-code all the various escapes.
See Description
Peter Deserves a Thumbs up.