简体   繁体   中英

Retina display and [UIImage initWithData]

I need to initialise images from raw data downloaded from a server which delivers the correct size of image based on the type of iPhone client.

I know that I should be setting the scale value to be 2.0 on the 640x960 display, however this is a readonly property and cannot be set during the init when using initWithData.

Any ideas?

I'm not aware of anything you can embed in the image data itself to tell the phone that it's a @2x image, but something like this should work:

UIImage * img = ...;
img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:img.imageOrientation];

由于iOS 6.0 UIImage有方法+ imageWithData:scale:你可以传递2.0作为视网膜的比例。

您可以将[[UIScreen mainScreen] scale]作为scale参数而不是2.0f

Swift3,4版本

let image = UIImage(data: imageData, scale: UIScreen.main.scale)

put this in your .m if you want or on an imported class (the syntax of c is nicer when calling the function IMAO)

BOOL isRetina(){
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        return [[UIScreen mainScreen] scale] == 2.0;
    }
    return NO;
}

Then when creating the image with the server data:

[UIImage imageWithData:dataFromServer scale:isRetina()?2:1];

AFAIK you don't need to set the scale value yourself. The OS will handle the points to pixel translation for you.

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