Find the origin of subview with respect to UIImage

2019-08-10 22:17发布

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.

3条回答
再贱就再见
2楼-- · 2019-08-10 22:59

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
}
查看更多
Animai°情兽
3楼-- · 2019-08-10 23:08

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;
查看更多
小情绪 Triste *
4楼-- · 2019-08-10 23:15

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.
查看更多
登录 后发表回答