“contentScaleFactor”背后的秘密是什么? UIView 与 CATiledLayer 一起使用时的效果如何?
您好,
我正在开发一个受 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
及其用途的更多信息。在阅读了所有提到它的苹果文档后,我搜索了谷歌,但从未找到它实际用途的明确答案。
以下是我好奇的一些事情:
当绘制 UIView/CALayer 的内容时,似乎
contentScaleFactor
对图形上下文有某种影响。这似乎与高分辨率显示器(如视网膜显示器)有关。contentScaleFactor
到底有什么样的效果以及对什么有影响?当使用
UIScrollView
并将其设置为缩放时,比方说,我的 contentView; contentView 的所有子视图也正在缩放。这是如何运作的?UIScrollView
会修改哪些属性,甚至使视频播放器变得模糊并放大?
TL;DR:UIScrollView 的缩放功能“在幕后”是如何工作的?我想了解它是如何工作的,以便我可以编写正确的代码。
任何提示和解释都非常感谢! :)
Greetings,
I'm working on an application inspired by the "ZoomingPDFViewer" example that comes with the iOS SDK. At some point I found the following bit of code:
// 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;
I tried to learn more about contentScaleFactor
and what it does. After reading everything of Apple's documentation that mentioned it, I searched Google and never found a definite answer to what it actually does.
Here are a few things I'm curious about:
It seems that
contentScaleFactor
has some kind of effect on the graphics context when a UIView's/CALayer's contents are being drawn. This seems to be relevant to high resolution displays (like the Retina Display). What kind of effect doescontentScaleFactor
really have and on what?When using a
UIScrollView
and setting it up to zoom, let's say, my contentView; all subviews of contentView are being scaled, too. How does this work? Which properties doesUIScrollView
modify to make even video players become blurry and scale up?
TL;DR: How does UIScrollView's zooming feature work "under the hood"? I want to understand how it works so I can write proper code.
Any hints and explanation is highly appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
坐标以点而非像素表示。
contentScaleFactor
定义点和像素之间的关系:如果为 1,则点和像素相同,但如果为 2(如视网膜显示器),则表示每个点有两个像素。在普通绘图中,使用点意味着您不必担心分辨率:在 iphone 3(scaleFactor 1)和 iphone4(scaleFactor 2 和 2x 分辨率)中,您可以使用相同的坐标和绘图代码。但是,如果您正在绘制图像(直接作为纹理......)并且仅使用法线坐标(点),则您不能相信该像素到点的映射是 1 比 1。如果您这样做,那么每个像素如果scaleFactor为2(x方向为2,y方向为2),则图像将对应于1个点,但对应于4个像素,因此图像可能会变得有点模糊
使用
CATiledLayer
,使用scalefactor 2可能会得到一些意想不到的结果我猜想UIView
的contentScaleFactor==2
和图层contentScale==2
会混淆系统,有时会使比例成倍增加。也许 Scrollview 也会发生类似的情况。希望这能澄清一点
Coordinates are expressed in points not pixels.
contentScaleFactor
defines the relation between point and pixels: if it is 1, points and pixels are the same, but if it is 2 (like retina displays ) it means that every point has two pixels.In normal drawing, working with points means that you don't have to worry about resolutions: in iphone 3 (scaleFactor 1) and iphone4 (scaleFactor 2 and 2x resolution), you can use the same coordinates and drawing code. However, if your are drawing a image (directly, as a texture...) and just using normal coordinates (points), you can't trust that pixel to point map is 1 to 1. If you do, then every pixel of the image will correspond to 1 point but 4 pixels if scaleFactor is 2 (2 in x direction, 2 in y) so images could became a bit blurred
Working with
CATiledLayer
you can have some unexpected results with scalefactor 2. I guess that having theUIView
acontentScaleFactor==2
and the layer acontentScale==2
confuse the system and sometimes multiplies the scale. Maybe something similar happens with Scrollview.Hope this clarifies it a bit
Apple 在 iOS 开发文档的“支持高分辨率屏幕”页面上有一个关于此的部分。
该页面说:
请参阅此处:支持高-分辨率屏幕
Apple has a section about this on its "Supporting High-Resolution Screens" page in the iOS dev documentations.
The page says:
See it here: Supporting High-Resolution Screens