当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS像素对齐概念解析

iOS像素对齐概念解析

2019年07月24日  | 移动技术网IT编程  | 我要评论

布莉安娜爱珍娜,皇族mc基地,0x000009

在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。

部分代码

  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];

 结论
 1.所谓的像素对齐,指的是物理像素对齐。
 2.如果是2x的图像,放在3x的屏幕上(6sp),也会发生像素不对齐的情况。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网