这篇文章主要为大家详细介绍了iOS像素对齐概念,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在iOS中,有一个概念叫做像素对齐,如果像素不对齐,那么在GPU渲染时,需要进行插值计算,这个插值计算的过程会有性能损耗。
在模拟器上,有一个选项可以把像素不对齐的部分显示出来。
[[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),也会发生像素不对齐的情况。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
本文标题为:iOS像素对齐概念解析


- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- iOS 对当前webView进行截屏的方法 2023-03-01
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android实现轮询的三种方式 2023-02-17
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Android studio实现动态背景页面 2023-05-23
- Flutter实现底部和顶部导航栏 2022-08-31
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android实现监听音量的变化 2023-03-30