iOS像素對(duì)齊概念解析
在iOS中,有一個(gè)概念叫做像素對(duì)齊,如果像素不對(duì)齊,那么在GPU渲染時(shí),需要進(jìn)行插值計(jì)算,這個(gè)插值計(jì)算的過(guò)程會(huì)有性能損耗。
在模擬器上,有一個(gè)選項(xiàng)可以把像素不對(duì)齊的部分顯示出來(lái)。
邏輯像素與物理像素
在iOS設(shè)備上,有point(邏輯像素)的概念,以及pixel(物理像素)的概念。
在編程序時(shí),用的是point,實(shí)際渲染時(shí)用的是pixel。一個(gè)point可以對(duì)應(yīng)多個(gè)pixel。
point和pixel的比例是可以通過(guò)[[UIScreen mainScreen] scale]來(lái)制定。
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.
就是說(shuō)image的size和image和scale相乘,得到物理像素的大小。
問(wèn)題
那么像素不對(duì)齊指的是物理像素(pixel)和邏輯像素(point)對(duì)齊呢?
實(shí)驗(yàn)
使用300*225像素的png圖片。分別使用不同的方法load到內(nèi)存中,得到不同的size和scale,然后放在不同size的imageview里。使用color misaligned images來(lái)判定是否像素對(duì)齊。
這里模擬器使用的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];
結(jié)論
1.所謂的像素對(duì)齊,指的是物理像素對(duì)齊。
2.如果是2x的圖像,放在3x的屏幕上(6sp),也會(huì)發(fā)生像素不對(duì)齊的情況。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 中exit(),_exit(),return,abort()函數(shù)的區(qū)別
這篇文章主要介紹了C++ 中exit(),_exit(),return,abort()函數(shù)的區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-12-12iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息(帶數(shù)字)
本文主要介紹了iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā)
本文主要介紹了ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07