I tried to display an image returned by Google Chart API, but the codes below do not work:
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];
imgView.image = downloadedImage;
or
NSData *imageData=[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]] returningResponse:nil error:nil];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
imgView.image = downloadedImage;
The target image was not shown as expected. Do you have any idea where the problem was?
Both pieces of code work, except the
NSURLobject is nil.NSURLdoes not support pipe characters (|), so you need to escape it with%7c. You could use[NSString stringByAddingPercentEscapesUsingEncoding:]to take care of any other characters. Here is the new version: