IOS开发入门之iOS开发之图片分辨率与像素对齐
凌雪 2018-09-17 来源 :网络 阅读 443 评论 0

摘要:本文将带你了解IOS开发入门之iOS开发之图片分辨率与像素对齐,希望本文对大家学Ios有所帮助。

本文将带你了解IOS开发入门之iOS开发之图片分辨率与像素对齐,希望本文对大家学Ios有所帮助。


像素对齐的概念
   
    在iOS中,有一个概念叫做像素对齐,如果像素不对齐,那么在GPU渲染时,需要进行插值计算,这个插值计算的过程会有性能损耗。
    在模拟器上,有一个选项可以把像素不对齐的部分显示出来。
    
   
    逻辑像素与物理像素
   
    在iOS设备上,有point(逻辑像素)的概念,以及pixel(物理像素)的概念。
    在编程序时,用的是point,实际渲染时用的是pixel。一个point可以对应多个pixel。
point和pixel的比例是可以通过[[UIScreen mainScreen] scale]来制定。
   
    UIImage的scale概念
   
   
    If you load an image from a file whose name   includes the @2x modifier, the scale is set to 2.0. You can also specify an   explicit scale factor when initializing an image from a Core Graphics image.   All other images are assumed to have a scale factor of 1.0.
   
   
    image也有size的概念。
   
   
    This value reflects the logical size of the   image and takes the image’s current orientation into account. Multiply the   size values by the value in the scale property to get the pixel dimensions of   the image.
   
   
    就是说image的size和image和scale相乘,得到物理像素的大小。
   
    问题
   
    那么像素不对齐指的是物理像素(pixel)和逻辑像素(point)对齐呢?
   
    实验
   
    使用300*225像素的png图片。分别使用不同的方法load到内存中,得到不同的size和scale,然后放在不同size的imageview里。使用color misaligned images来判定是否像素对齐。
    这里模拟器使用的iPhone 6,屏幕的 scale是2。
   
   
   
   
    image size
    image scale
    imageview Size
    是否misalign
   
   
   
   
   
    300,225
    1
    300,225
    是
   
   
    300,225
    1
    150,112.5
    否
   
   
    150,112.5
    2
    150,112.5
    否
   
   
    150,112.5
    2
    75,56.25
    是
   
   
   
   
    部分代码
   
          NSLog(@"screen scale is %f",[[UIScreen mainScreen]   scale]);
    UIImage *image = [UIImage   imageNamed:@"test.png"];
    NSLog(@"image size %@, scale   %f ", [NSValue valueWithCGSize:image.size], image.scale);
    UIImageView *imageView =   [[UIImageView alloc] initWithImage:image];
    //    imageView.frame =   CGRectMake(50, 100, imageView.bounds.size.width * 2/3,   imageView.bounds.size.height * 2/3);
    imageView.frame = CGRectMake(50,   100, imageView.bounds.size.width, imageView.bounds.size.height);
    NSLog(@"imageView frame   %@", [NSValue valueWithCGRect:imageView.frame]);
    [self.view addSubview:imageView];
   
   
    结论
   
   
    所谓的像素对齐,指的是物理像素对齐。
    如果是2x的图像,放在3x的屏幕上(6sp),也会发生像素不对齐的情况。
   
        

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程