Resize Google chart image to fit in iPhone
Please open this link in any Web Browser and in iPhone (Simulator will do)
https://chart.googleapis.com/chart?chs=600x225&cht=p&chtt=Consumption+by+Appliances&chdl=Other+$4.62|Pool%20Pump+$6.51|Refrigerator+$1.51|Clothes%20Dryer+$1.50|Always%20On+$7.12&chl=Other+21%|Pool%20Pump+30%|Refrigerator+7%|Clothes%20Dryer+7%|Always%20On+33%&chd=t:4.62252,6.51,1.5072,1.50252,7.1232&chds=0,7.1232
The width of the image is way to big to fit in iPhone (600 px). However I want to resize it to fit in iPhone screen. I tried the following resizing code but no success.
CGRect area = CGRectMake(0, 0, newWidth, newHeight);
CGSize size = area.size;
UIGraphicsBeginImageContext(size);
[oldImage drawInRect:area];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I know that instead of 600 px if write 320 px it will fit in the screen but my problem is I am getting the image from a web server which uses google charts to generate chart(as PNG image) and I will receive the google chart image url from my web server which I cannot adjust.
I am storing the received URL image in UIImage using following code
NSURL *url = [NSURL URLWithString:urlAddress];
UIImage *image = [[UIImage alloc] initWithDat开发者_高级运维a: [NSData dataWithContentsOfURL:url]];
So is there any way to reszie the received image so that it can fit completely in the screen
Note: I don't want scrolling to be done to view image. I want to fit it completely at once.
Thanks in advance.
Why cant you change the Size in the URL itself
https://chart.googleapis.com/chart?chs=300x200&chd=t:60,40&cht=p3&chl=Hello|World
Give whatever resolution which fits you.
If your question is 'how can I change what I get from the server' I don't think this is an iOS question. And @HaveyouMetPrabu's answer looks great to me.
If your question is 'what's the best way to resize this image on the iOS device' then I'd say Image I/O.
A word of caution, the code;
NSURL *url = [NSURL URLWithString:urlAddress];
UIImage *image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL:url]];
blocks while the data is being downloaded. Which sitting in your office on a fast Wifi will be no problem, but out in the field will lead to your app begin killed by the watchdog.
精彩评论