CATiledLayer 得到一个不正确的框架& contextCTM.a 值 (zoomScale)

发布于 2024-10-10 21:02:51 字数 880 浏览 12 评论 0原文

在 WWDC2010 Session 104 之后,我刚刚在 UIScrollView 中为 10000 x 8078 px 图像创建了一个 CATiledLayer。

我对框架和框架感到困扰zoomScale 当视图首次显示在其顶层

  1. (如会话 104 中)时,我
  2. 在我的绘制矩形中将 levelsOfDetail 定义为 4,我调用 CGFloat lScale = CGContextGetCTM(lContext)。 a;

奇怪的是,在运行时lScale被分配为0.124907158,而不是预期的0.125。

此外,传递给 drawRectrect 也具有浮点值,例如

  • rect.origin.x = 4099.04443
  • rect.origin.y = 6144
  • rect.size.width = 2049.52222
  • rect。 size.height = 2048

最让我恼火的是,CATiledLayer 的框架显示原点 0 x 26.93,即使我使用 initWithFrame 使用 0x0 原点创建了tiledImageView。

我有什么想法可以找到对此负责的计算吗?


编辑: 我找到了奇怪的框架位置的来源,即 ScrollView 边界被错误地设置为 450 像素。那应该是 420 像素。 UIScrollView 中的layoutSubview 例程有一个中心视图例程,它无意中调整了y 坐标。

following WWDC2010 Session 104 I just created a CATiledLayer within a UIScrollView for a 10000 x 8078 px image.

I am troubled with the frame & zoomScale when the view is first shows at its top level

  1. like in session 104 I defined levelsOfDetail as 4
  2. in my drawRect I call CGFloat lScale = CGContextGetCTM(lContext).a;

Strangely at runtime lScale is assigned 0.124907158, not 0.125 as expected.

As well the rect passed to drawRect has floatpoint values, such as

  • rect.origin.x = 4099.04443
  • rect.origin.y = 6144
  • rect.size.width = 2049.52222
  • rect.size.height = 2048

Most irritating for me is that the frame of the CATiledLayer shows an origin 0 x 26.93 even though I created the tiledImageView using initWithFrame using a 0x0 origin.

Any ideas where I can find the calculation that is responsible for this?


EDIT:
I found the source for the wierd frame position, which is the ScrollView bounds being incorrectly set at 450 px. that should be 420 px. The layoutSubview routine in the UIScrollView has a center view routine that inadvertently adjusted the y coordinate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

巨坚强 2024-10-17 21:02:51

我找到了解决这个问题的方法。

WWDC 2010 会话 104 示例有两个控制视图行为的 UIScrollView 方法。

第一个是 configureForImageSize,我在之前发布的答案中提到过,其中设置了缩放比例。

由于所解释的浮点差异,生成的 imageView 边界带有之前突出显示的小数(请参阅 wx/hx 或 wy/hy)。

为了清理这个问题,我使用了 layoutSubviews 的覆盖来计算视图框架:

- (void) layoutSubviews {
    [super layoutSubviews];

    CGSize lBoundsSize = self.bounds.size;
    CGRect lFrameToCenter = imageView.frame;

    // cure to the decimals problem
    lFrameToCenter.size.width  = roundf(lFrameToCenter.size.width);
    lFrameToCenter.size.height = roundf(lFrameToCenter.size.height);

    if (lFrameToCenter.size.width < lBoundsSize.width)
        lFrameToCenter.origin.x = (lBoundsSize.width - lFrameToCenter.size.width) / 2;
    else
        lFrameToCenter.origin.x = 0;

    if (lFrameToCenter.size.height < lBoundsSize.height)
        lFrameToCenter.origin.y = (lBoundsSize.height - lFrameToCenter.size.height) / 2;
    else
        lFrameToCenter.origin.y = 0;

    imageView.frame = lFrameToCenter;

    if ([imageView isKindOfClass:[tileView class]]) {
        imageView.contentScaleFactor = 1.0;
    }
}

请注意,我在对其进行任何其他操作之前对框架大小进行了舍入!

I found a cure to this.

The WWDC 2010 session 104 sample has two UIScrollView methods that control the view behaviour.

The first is configureForImageSize that I mentioned in my previously posted answer where the zoomscales are set.

Due to the explained float-point difference the imageView-bounds get generated with the decimals as highlighted before (see wx/hx or wy/hy).

To get clean this up I used the override of layoutSubviews where I calculate the view frame:

- (void) layoutSubviews {
    [super layoutSubviews];

    CGSize lBoundsSize = self.bounds.size;
    CGRect lFrameToCenter = imageView.frame;

    // cure to the decimals problem
    lFrameToCenter.size.width  = roundf(lFrameToCenter.size.width);
    lFrameToCenter.size.height = roundf(lFrameToCenter.size.height);

    if (lFrameToCenter.size.width < lBoundsSize.width)
        lFrameToCenter.origin.x = (lBoundsSize.width - lFrameToCenter.size.width) / 2;
    else
        lFrameToCenter.origin.x = 0;

    if (lFrameToCenter.size.height < lBoundsSize.height)
        lFrameToCenter.origin.y = (lBoundsSize.height - lFrameToCenter.size.height) / 2;
    else
        lFrameToCenter.origin.y = 0;

    imageView.frame = lFrameToCenter;

    if ([imageView isKindOfClass:[tileView class]]) {
        imageView.contentScaleFactor = 1.0;
    }
}

Note that I round the frame size before doing anything else with it!

与酒说心事 2024-10-17 21:02:51

我对错误分析有了进一步的了解。

UIScrollView 中 mImageSize 为 10000x8078 和 320x450 边界,我使用会话 104 示例中的 configureForImageSize 计算,如下所示:

- (void) configureForImageSize:(CGSize)mImageSize  {
    CGSize lBoundsSize = [self bounds].size;

    // set up our content size and min/max zoomscale
    CGFloat lXScale = lBoundsSize.width / mImageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat lYScale = lBoundsSize.height / mImageSize.height;  // the scale needed to perfectly fit the image height-wise

    // debug values
    float wx = mImageSize.width  * lXScale;
    float wy = mImageSize.width  * lYScale;
    float hx = mImageSize.height * lXScale;
    float hy = mImageSize.height * lYScale;

    ...
}

调试器显示以下值:

* mImageSize = width 10000 height 8078
* bounds size: width 320 height 450
* lXScale = 0.0396137647
* lYScale = 0.0450000018
* wx = 320
* wy = 363.51001
* hx = 396.137634
* hy = 450.000031

因此我收集图像与边界相关的大小会导致浮点计算差异。当生成CATiledLayer时,边界始终是浮点值。

如果这是原因,我怎样才能得到一个数学方法,而不必调整图像大小(丑陋的想法)???

I got a little further in my bug analysis.

Having an mImageSize of 10000x8078 and 320x450 Bounds in the UIScrollView I used the configureForImageSize calculations from the session 104 example as follows:

- (void) configureForImageSize:(CGSize)mImageSize  {
    CGSize lBoundsSize = [self bounds].size;

    // set up our content size and min/max zoomscale
    CGFloat lXScale = lBoundsSize.width / mImageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat lYScale = lBoundsSize.height / mImageSize.height;  // the scale needed to perfectly fit the image height-wise

    // debug values
    float wx = mImageSize.width  * lXScale;
    float wy = mImageSize.width  * lYScale;
    float hx = mImageSize.height * lXScale;
    float hy = mImageSize.height * lYScale;

    ...
}

The debugger shows the following values:

* mImageSize = width 10000 height 8078
* bounds size: width 320 height 450
* lXScale = 0.0396137647
* lYScale = 0.0450000018
* wx = 320
* wy = 363.51001
* hx = 396.137634
* hy = 450.000031

so I gather the image size in relation to the bounds leads to a floatpoint calculation difference. When the CATiledLayer is generated the bounds are alway floatpoint values.

If that is the cause, how can I get a mathematical way out of this without having to resize the image (ugly thought)???

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