找到子视图相对于UIImage尺寸的原点[英] Find the origin of subview with respect to UIImage size

本文是小编为大家收集整理的关于找到子视图相对于UIImage尺寸的原点的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是层次结构:

-- ViewController.View P [width: 375, height: 667]
---- UIImageView A       [width: 375, height: 667]
                         [A is holding an image of size(1287,1662)]
---- UIImageView B       [width: 100, height: 100]
                         [B is holding an image of size(2400,982)]

注意:b不是A的子视图.在应用程序中,b是可拖动的.我试图合并两个位置的位置,A [background]和b [前景].

我想找到B的确切位置,以使其合并后,它将处于我拖动和掉落的位置.

  1. 我已经写了所有的拖放代码.
  2. 我找不到与A.
  3. 合并的合适位置

这是问题屏幕截图:


显示b.

的初始位置

显示b.

的初始位置

这是拖放后B的新位置.

这是b.

的新位置

合并后B的错误位置.

合并后B的错误位置.

任何帮助将不胜感激.

预先感谢.

注意! :稍后附加的图像示例,并在Google搜索中建立了屏幕截图内的签名.我仅用于演示目的.

推荐答案

因为ImageView的大小与原始图像不同,而不是通过两个视图的绝对位置合并两个图像,因此可以使用相对位置.

sudo代码:

let relativeOrigin = {(B's origin.x in A) / A.width , (B's origin.y in A) / A.height}
let actualOrigin = { imgAWidth * relativeOrigin.x , imgAHeight * relativeOrigin.y } //this should be the correct absolute origin.

其他推荐答案

您可以将其视为建议:

FullscreenPoint.frame.origin.y = (screenShot.frame.origin.y*FullImage.frame.size.height)/screenShot.frame.size.height;                        
FullscreenPoint.frame.origin.x = (screenShot.frame.origin.x*FullImage.frame.size.width)/screenShot.frame.size.width;

其他推荐答案

我需要使用ImageView's(a)frame.size而不是a.image.size合并时获取正确的位置.

这是代码:

func mixImagesWith(frontImage:UIImage?, backgroundImage: UIImage?, atPoint point:CGPoint, ofSize signatureSize:CGSize) -> UIImage {
    let size = self.imgBackground.frame.size
    //let size = self.imgBackground.image!.size
    UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
    backgroundImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
    frontImage?.draw(in: CGRect.init(x: point.x, y: point.y, width: signatureSize.width, height: signatureSize.height))
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage
}

本文地址:https://www.itbaoku.cn/post/2091174.html

问题描述

This is the hierarchy:

-- ViewController.View P [width: 375, height: 667]
---- UIImageView A       [width: 375, height: 667]
                         [A is holding an image of size(1287,1662)]
---- UIImageView B       [width: 100, height: 100]
                         [B is holding an image of size(2400,982)]

Note: B is not a subview of A. In the app, B is draggable. And I am trying to merge two A and B where positions are, A [background] and B [foreground].

I want to find the exact position of B such that, after merging it will be at the same position where I dragged & dropped.

  1. I have written all the code for drag and drop.
  2. I couldn't find a proper position of B for merging with A.

Here are the problem screenshots:


Showing the initial position of B.

Showing the initial position of B.


This is the new position of B after drag and drop.

This is the new position of B.


Wrong position of B after merging.

Wrong position of B after merging.

Any help would be appreciated.

Thanks in advance.

Attention! : Attached images Sample Later and a Signature inside the screenshots has been founded in Google search. I have used for demonstration purpose only.

推荐答案

Because the imageView's size is different from the original image ,instead of merging two images via two view's ABSOLUTE position , you can use a RELATIVE position.

Sudo-code :

let relativeOrigin = {(B's origin.x in A) / A.width , (B's origin.y in A) / A.height}
let actualOrigin = { imgAWidth * relativeOrigin.x , imgAHeight * relativeOrigin.y } //this should be the correct absolute origin.

其他推荐答案

You can consider it as a suggestion:

FullscreenPoint.frame.origin.y = (screenShot.frame.origin.y*FullImage.frame.size.height)/screenShot.frame.size.height;                        
FullscreenPoint.frame.origin.x = (screenShot.frame.origin.x*FullImage.frame.size.width)/screenShot.frame.size.width;

其他推荐答案

I needed to use imageView's (A) frame.size instead of A.image.size to get the right location while merging.

Here's the code:

func mixImagesWith(frontImage:UIImage?, backgroundImage: UIImage?, atPoint point:CGPoint, ofSize signatureSize:CGSize) -> UIImage {
    let size = self.imgBackground.frame.size
    //let size = self.imgBackground.image!.size
    UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
    backgroundImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
    frontImage?.draw(in: CGRect.init(x: point.x, y: point.y, width: signatureSize.width, height: signatureSize.height))
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage
}