日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之图片分辨率与像素对齐

發布時間:2025/4/9 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 sizeimage scaleimageview Size是否misalign
300,2251300,225
300,2251150,112.5
150,112.52150,112.5
150,112.5275,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),也會發生像素不對齊的情況。
  • 轉載于:https://www.cnblogs.com/huahuahu/p/iOS-kai-fa-zhi-tu-pian-fen-bian-lu-yu-xiang-su-dui.html

    總結

    以上是生活随笔為你收集整理的iOS开发之图片分辨率与像素对齐的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。