ios开发之显示一个真实长宽为4X2厘米的图片
单位:
px:像素,构成影像的最小单位
ppi:Pixels Per Inch,即每英寸所拥有的像素数目,屏幕像素密度
pt:iOS开发单位,即point,
我们先求一下ppi
公式:
iPhone 3G 的 ppi 计算:
iPhone 3G 的屏幕像素密度 ppi 是每英寸 163 个像素,而苹果又规定在这种非视网膜的 iPhone 上,1pt=1px,所以我们也可以说每英寸上有 163 个 pt,那么 1pt=1/163 英寸,这就是 iOS 开发单位 pt 的长度:1pt = 1 /163 英寸 * 25.4毫米 ≈ 0.16 毫米。
所以根据上面的公式进行计算:
//获取手机的屏幕尺寸(英寸)
+ (double)deviceTypeSizeInches
{
structutsnamesystemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
//------------------------------iPhone---------------------------
//"iPhone 2G"
if([platformisEqualToString:@"iPhone1,1"])return3.5;
//"iPhone 3G"
if([platformisEqualToString:@"iPhone1,2"])return3.5;
//"iPhone 3GS"
if([platformisEqualToString:@"iPhone2,1"])return3.5;
//"iPhone 4"
if([platformisEqualToString:@"iPhone3,1"] ||
[platformisEqualToString:@"iPhone3,2"] ||
[platformisEqualToString:@"iPhone3,3"])return3.5;
//"iPhone 4S"
if([platformisEqualToString:@"iPhone4,1"])return3.5;
//"iPhone 5"
if([platformisEqualToString:@"iPhone5,1"] ||
[platformisEqualToString:@"iPhone5,2"])return4.0;
//"iPhone 5c"
if([platformisEqualToString:@"iPhone5,3"] ||
[platformisEqualToString:@"iPhone5,4"])return4.0;
//"iPhone 5s"
if([platformisEqualToString:@"iPhone6,1"] ||
[platformisEqualToString:@"iPhone6,2"])return4.0;
//"iPhone 6"
if([platformisEqualToString:@"iPhone7,2"])return4.7;
//"iPhone 6 Plus"
if([platformisEqualToString:@"iPhone7,1"])return5.5;
//"iPhone 6s"
if([platformisEqualToString:@"iPhone8,1"])return4.7;
//"iPhone 6s Plus"
if([platformisEqualToString:@"iPhone8,2"])return5.5;
//"iPhone SE"
if([platformisEqualToString:@"iPhone8,4"])return4.0;
//"iPhone 7"
if([platformisEqualToString:@"iPhone9,1"] ||
[platformisEqualToString:@"iPhone9,3"])return4.7;
//"iPhone 7 Plus"
if([platformisEqualToString:@"iPhone9,2"] ||
[platformisEqualToString:@"iPhone9,4"])return5.5;
//"iPhone 8"
if([platformisEqualToString:@"iPhone10,1"] ||
[platformisEqualToString:@"iPhone10,4"])return4.7;
//"iPhone 8 Plus"
if([platformisEqualToString:@"iPhone10,2"] ||
[platformisEqualToString:@"iPhone10,5"])return5.5;
//"iPhone X"
if([platformisEqualToString:@"iPhone10,3"] ||
[platformisEqualToString:@"iPhone10,6"])return5.8;
//"iPhone XR"
if([platformisEqualToString:@"iPhone11,8"])return6.1;
//"iPhone XS"
if([platformisEqualToString:@"iPhone11,2"])return5.8;
//"iPhone XS Max"
if([platformisEqualToString:@"iPhone11,4"] ||
[platformisEqualToString:@"iPhone11,6"])return6.5;
//"iPhone 11
if([platformisEqualToString:@"iPhone12,1"])return6.1;
//"iPhone 11 Pro"
if([platformisEqualToString:@"iPhone12,3"])return5.8;
//"iPhone 11 Pro Max"
if([platformisEqualToString:@"iPhone12,5"])return6.5;
return 4.0;
}
////////////////
_cheimageview.image=[UIImage imageNamed:@"icon-01.jpg"];
//获取手机的屏幕尺寸(英寸)
double pmsize=[GlobalValue deviceTypeSizeInches];
//计算ppi
double ppi = sqrt( Screen_Width*Screen_Width + Screen_Height*Screen_Height ) / pmsize ;
NSLog(@"宽度:%f;ppi:%f",Screen_Width,ppi);
//iphoneX 宽度:375.000000;ppi:154.208597
//iphone SE 宽度:320.000000;ppi:162.984662
//
//154.208597/162.984662*192.55;
//pt 的长度(毫米)
doublept=1.0/ppi *25.4;
NSString*str=[NSStringstringWithFormat:@"%.4f", pt];
doubleff=[strdoubleValue];
_cheimageWidth.constant=40.0/ff;
_cheimageHeight.constant=20.0/ff;
NSLog(@"图片宽度:%f",40.0/ff);
这样就可以实现上面的需求了。
文章参考了:
网友评论