【问题标题】:Get position of Image from inside UIImageView to Window's Coordinate从 UIImageView 内部获取图像的位置到窗口的坐标
【发布时间】:2021-10-02 00:38:55
【问题描述】:

我有一个 imageView,它的 frame 是 (47.0, 415.66666666666674, 320.0, 320.0),它的 y 原点是 415

lazy var imageView: UIImageView = {
   let imgView = UIImageView()
   // ...
   imgView.contentMode = .aspectScaleFit
}

要在我使用的 imageView 中获取图像的矩形:

view.layoutIfNeeded()

let imageRect = AVMakeRect(aspectRatio: imageView.image!.size, insideRect: imageView.bounds)

print(imageRect) // (-47.0, -358.2753623188407, 320.0, 205.21739130434787)

当我尝试转换 imageRect 以找出它在 Window 坐标系内的位置时,我得到了

let frameInWindow = imageView.convert(imageRect, from: nil)

print(frameInWindow) // (-47.0, -358.2753623188407, 320.0, 205.21739130434787)

我有两个问题:

1- 为什么 xy 的值为负值 -47-358

为了获得正的 y 值,我使用了 let yPosition = abs(frameInWindow.origin.y),这给了我一个 358 的 yPosition,但是它在 imageView 的 y 位置之上。

2-为什么 yPosition 不是 425 或者考虑到图像的中心设置在 imageView 的中心的任何位置,并且因为图像小于 imageView,它的 y.origin 应该低于 415( Window内imageView的y值)

【问题讨论】:

    标签: ios swift uiimageview cgrect


    【解决方案1】:

    当您说imageView.convert(imageRect, from: nil) 时,您正在将imageRect 从窗口的坐标转换为图像视图的坐标。但是imageRect 不在窗口的坐标中,所以这没有意义。

    【讨论】:

    • 有道理,但我不能这样做:let frameInWindow = imageView.image.convert(imageRect, from: nil)
    • 没错。没有什么可以转换的。图像不是视图。
    • 但我确实有图像的矩形,那么如何将该矩形转换为窗口? let imageRect = AVMakeRect(aspectRatio: imageView.image!.size, insideRect: imageView.bounds)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2019-07-30
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多