“contentScaleFactor”背后的秘密是什么? UIView 与 CATiledLayer 一起使用时的效果如何?

发布于 2024-10-21 14:07:27 字数 1024 浏览 2 评论 0原文

您好,

我正在开发一个受 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 及其用途的更多信息。在阅读了所有提到它的苹果文档后,我搜索了谷歌,但从未找到它实际用途的明确答案。

以下是我好奇的一些事情:

  1. 当绘制 UIView/CALayer 的内容时,似乎 contentScaleFactor 对图形上下文有某种影响。这似乎与高分辨率显示器(如视网膜显示器)有关。 contentScaleFactor 到底有什么样的效果以及对什么有影响?

  2. 当使用 UIScrollView 并将其设置为缩放时,比方说,我的 contentViewcontentView 的所有子视图也正在缩放。这是如何运作的? 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:

  1. 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 does contentScaleFactor really have and on what?

  2. 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 does UIScrollView 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡月浅 2024-10-28 14:07:27

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

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

使用CATiledLayer,使用scalefactor 2可能会得到一些意想不到的结果我猜想 UIViewcontentScaleFactor==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 the UIView a contentScaleFactor==2 and the layer a contentScale==2 confuse the system and sometimes multiplies the scale. Maybe something similar happens with Scrollview.

Hope this clarifies it a bit

地狱即天堂 2024-10-28 14:07:27

Apple 在 iOS 开发文档的“支持高分辨率屏幕”页面上有一个关于此的部分。

该页面说:

更新您的自定义绘图代码

当您在应用程序中进行任何自定义绘图时,大多数时候
你不需要关心底层的分辨率
屏幕。原生绘图技术自动确保
您在逻辑坐标空间图中正确指定的坐标
到底层屏幕上的像素。然而,有时您可能需要
了解当前的比例因子是多少,以便渲染您的
内容正确。对于这些情况,UIKit、Core Animation 和
其他系统框架为您提供绘图所需的帮助
正确。

以编程方式创建高分辨率位图图像如果您
目前使用UIGraphicsBeginImageContext函数来创建
位图,您可能需要调整代码以将比例因子考虑在内
帐户。 UIGraphicsBeginImageContext 函数总是创建
比例因子为 1.0 的图像。如果底层设备有
高分辨率屏幕,使用此功能创建的图像可能不会
渲染时显得平滑。创建具有比例因子的图像
除 1.0 之外,请使用 UIGraphicsBeginImageContextWithOptions
反而。使用该功能的过程与使用该功能的过程相同
UIGraphicsBeginImageContext函数:

  1. 调用 UIGraphicsBeginImageContextWithOptions 创建位图
    上下文(具有适当的比例因子)并将其推到
    图形堆栈。
  2. 使用 UIKit 或 Core Graphics 例程来绘制内容
    图片。
  3. 调用 UIGraphicsGetImageFromCurrentImageContext 获取位图
    内容。
  4. 调用 UIGraphicsEndImageContext 从堆栈中弹出上下文。

例如下面的代码片段
创建一个 200 x 200 像素的位图。 (像素数为
通过将图像的大小乘以比例来确定
因素。)

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

请参阅此处:支持高-分辨率屏幕

Apple has a section about this on its "Supporting High-Resolution Screens" page in the iOS dev documentations.

The page says:

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);

See it here: Supporting High-Resolution Screens

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文