views:

195

answers:

1

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

+2  A: 

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

Ty
Or just use `stringByAddingPercentEscapesUsingEncoding:`. Then you don't have to hard-code all the various escapes. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAddingPercentEscapesUsingEncoding:
Peter Hosey
Peter Hosey has the right answer for this one.. You should post as an answer, so that people can give you points..
Eduardo Scoz