문제

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

도움이 되었습니까?

해결책

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

stringByAddingPercentEscapesUsingEncoding:

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

Peter Deserves a Thumbs up.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top