简体   繁体   中英

Reading a image at 2x or 3x in iOS

I am fetching a image from my server based on the scale so I fetch something like :

http://myserver.com/image1.png

or http://myserver.com/image1@2x.png

However what I see is that once I initialize a image with the contents of http://myserver.com/image1@2x.png the scale on the UIImage says it is 1x and it gets rendered badly where I want it to be rendered, it renders it in full size.. instead of 1/2 the size with double the pixels.. how do I make this work correctly?

You can create a new UIImage with a scale factor of 2 using the code below:

UIImage * img = [UIImage imageNamed:@"myimagename"];
img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:img.imageOrientation];

To make the code device-independent, you should get the scale factor of the current device using the code below and replace the number 2 with it.

[UIScreen mainScreen].scale

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM