本文介绍了如何使convertToDeviceSpace进行除乘一以外的任何操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 CALayer

override open func draw(in ctx: CGContext) {

    let rectDEVICESPACE = ctx.convertToDeviceSpace(bounds).size

    print("bounds \(bounds)")
    print("rectDEVICESPACE \(rectDEVICESPACE)")

尝试了很多很多之后分钟,我们发现找不到任何变换或比例。

After trying for many, many, many minutes we have found it impossible to find any transform or scale.

convertToDeviceSpace 总是返回相同的内容。

convertToDeviceSpace always just returns the same thing.

尝试过大量的设备,模拟器等。

Tried mountains of devices, simulators, etc etc.

具体的实际情况是什么,其中 convertToDeviceSpace 会做身份以外的任何事情?

What is a specific actual situation where convertToDeviceSpace will do anything other than identity?

这很可怕,因为Apple文档和所有现有的c例如,当您四处张望时计算出原始像素大小时,ode样本将使用精确的样本。错误将是一个巨大的缺陷。

This is terrifying since the Apple documentation and all existing code samples use exactly that when, for example, figuring out the raw pixel size when you're blitting around. An error would be an enormous flaw.

什么是特定的实际情况,其中 convertToDeviceSpace 除了乘以1.0以外,还会做其他事情吗?

What is a specific actual situation where convertToDeviceSpace will do anything other than multiply by 1.0?

我在做错什么,特别是在测试中-除了身份,我再也看不到任何结果吗?

What am I doing wrong, specifically, in testing - that I never see any result other than just identity?

推荐答案

我将回答我自己的问题,以帮助所有Google员工:

I'll answer my own question, to help any googlers:

实际上

这确实意味着

自此!

基本上convertToDeviceSpace返回UIView大小,次, contentsScale

Basically convertToDeviceSpace returns the UIView size, times, contentsScale.

{此外,Apple在获得&q时将进行的任何将来未知的计算实际的,物理的像素大小。}

{Plus, any future unknowable calculations which Apple will make, in getting the "actual, physical" pixels size.}

似乎是一个鲜为人知的事实:

It seems to be a little-known fact that:

在iOS中,当您创建自定义图层时,并假设您要重新以像素为单位平滑绘制,实际上您必须设置contentsScale-并且您必须在初始化时执行该操作。 (TBC,一旦已经在上下文中调用过绘制,这样做将是一个严重的错误。)

in iOS when you make a custom layer, and assuming you're drawing it smoothly pixel-wise, in fact YOU must set the contentsScale - and YOU must do that at initialization time. (TBC, it would be a severe mistake do it once draw has already been called on the context.)

class PixelwiseLayer: CALayer {

    override init() {

        super.init()
        // SET THE CONTENT SCALE AT INITIALIZATION TIME
        contentsScale = UIScreen.main.scale
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

这里是对该问题的广泛检查:

Here's a vast examination of the issue: https://stackoverflow.com/a/47760444/294884

这篇关于如何使convertToDeviceSpace进行除乘一以外的任何操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:34