[英]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. 我需要从服务器下载的原始数据初始化图像,该服务器根据iPhone客户端的类型提供正确的图像大小。
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. 我知道我应该在640x960显示器上将比例值设置为2.0,但这是一个只读属性,并且在初始化时使用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: 我不知道你可以嵌入到图像数据中的任何东西,告诉手机它是一个@ 2x图像,但这样的东西应该有效:
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) 如果你想要或在导入的类上放入你的.m(调用函数IMAO时c的语法更好)
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. AFAIK您不需要自己设置比例值。 The OS will handle the points to pixel translation for you.
操作系统将为您处理点到像素的转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.