从 url 加载 NSImage 但只有部分 url 有效

发布于 2024-08-29 06:33:56 字数 767 浏览 2 评论 0原文

我有一些代码可以从网络上加载图像文件并将其放入图像视图中。问题是它适用于除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-09-05 06:33:56

这是失败的,因为 |不是有效的 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

萌逼全场 2024-09-05 06:33:56

或者只是使用

stringByAddingPercentEscapesUsingEncoding:

那么您不必对所有各种转义进行硬编码。
查看说明

彼得值得竖起大拇指。

Or just use

stringByAddingPercentEscapesUsingEncoding:

Then you don't have to hard-code all the various escapes.
See Description

Peter Deserves a Thumbs up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文