UIView中的“contentScaleFactor”与CATiledLayer一起使用时,背后的秘密是什么?

20

问候,

我正在开发一个应用程序,受到iOS SDK中附带的“ZoomingPDFViewer”示例的启发。在某个时刻,我发现了下面这段代码:

// to handle the interaction between CATiledLayer and high resolution
// screens, we need to manually set the tiling view's 
// contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 
// on high resolution screens, which would cause the CATiledLayer 
// to ask us for tiles of the wrong scales.)
pageContentView.contentScaleFactor = 1.0;

我试图了解contentScaleFactor及其作用。在阅读了苹果文档中提到的所有内容后,我搜索Google并没有找到确切的答案。

以下是我感兴趣的几件事:

  1. 似乎在绘制UIView/CALayer内容时,contentScaleFactor对图形上下文有某种影响。这似乎与高分辨率显示器(如Retina显示器)有关。 contentScaleFactor真正的作用是什么?它会对什么产生影响?

  2. 使用一个UIScrollView并设置缩放时,例如我的contentViewcontentView的所有子视图也会被缩放。这是如何实现的?UIScrollView修改哪些属性使得视频播放器变得模糊并进行缩放?

简而言之:UIScrollView的缩放功能“底层”是如何工作的? 我想理解它的运行方式,以便编写正确的代码。

非常感谢任何提示和解释! :)

2个回答

9

坐标是以点而不是像素表示的。 contentScaleFactor 定义了点和像素之间的关系:如果它是1,则点和像素相同,但如果它是2(例如视网膜显示器),则意味着每个点有两个像素。

在常规绘图中,使用点意味着您无需担心分辨率:在 iPhone 3(scaleFactor为1)和 iPhone 4(scaleFactor为2且2倍分辨率)中,您可以使用相同的坐标和绘图代码。但是,如果您正在绘制图像(直接作为纹理...)并且仅使用普通坐标(点),则不能信任像素到点映射是1对1的。如果这样做,那么图像的每个像素将对应于1个点,但是如果scaleFactor为2(x方向为2,y方向为2)则会对应于4个像素,因此图像可能会变得有些模糊。

使用 CATiledLayer 时,scalefactor为2可能会产生一些意外的结果。我猜想,将 UIViewcontentScaleFactor==2 和层的 contentScale==2 混淆可能会使系统有时乘以比例。ScrollView 可能会发生类似的情况。

希望这有点澄清了。


很棒的答案,这里有最新的iPhone分辨率总结、点数、比例因素和分辨率。http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions - Maxim Veksler

2

苹果在iOS开发文档的“支持高分辨率屏幕”页面中有相关内容。

该页面表示:

Updating Your Custom Drawing Code

When you do any custom drawing in your application, most of the time you should not need to care about the resolution of the underlying screen. The native drawing technologies automatically ensure that the coordinates you specify in the logical coordinate space map correctly to pixels on the underlying screen. Sometimes, however, you might need to know what the current scale factor is in order to render your content correctly. For those situations, UIKit, Core Animation, and other system frameworks provide the help you need to do your drawing correctly.

Creating High-Resolution Bitmap Images Programmatically If you currently use the UIGraphicsBeginImageContext function to create bitmaps, you may want to adjust your code to take scale factors into account. The UIGraphicsBeginImageContext function always creates images with a scale factor of 1.0. If the underlying device has a high-resolution screen, an image created with this function might not appear as smooth when rendered. To create an image with a scale factor other than 1.0, use the UIGraphicsBeginImageContextWithOptions instead. The process for using this function is the same as for the UIGraphicsBeginImageContext function:

  1. Call UIGraphicsBeginImageContextWithOptions to create a bitmap context (with the appropriate scale factor) and push it on the graphics stack.
  2. Use UIKit or Core Graphics routines to draw the content of the image.
  3. Call UIGraphicsGetImageFromCurrentImageContext to get the bitmap’s contents.
  4. Call UIGraphicsEndImageContext to pop the context from the stack.

For example, the following code snippet creates a bitmap that is 200 x 200 pixels. (The number of pixels is determined by multiplying the size of the image by the scale factor.)

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0,100.0), NO, 2.0);

在这里查看:支持高分辨率屏幕


4
请注意,将0.0作为比例传递给UIGraphicsBeginImageContextWithOptions函数会导致使用设备主屏幕的contentScaleFactor。在我看来,这是最简单的选项,可充分利用任何分辨率设备。这将自动为“普通”设备使用1.0,并为Retina设备使用2.0。非常简单易行! - PapillonUK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接