Find the origin of a subview regarding the size of a UIImage

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 subparagraph A. In Appendix B, it is dragged. And I'm trying to combine the two A and B, where the positions A [background] and B [foreground] are located.

I want to find the exact position B, so that after the merger she was in the same position in which I was dragged and dropped.

  • I wrote all the drag and drop code.
  • I could not find the correct position B for merging with A.

Here are the screenshots of the problem:


Starting position display B.

Showing the initial position of B.


This is the new position B after drag and drop.

This is the new position of B.


Wrong position B after merger.

Wrong position of B after merging.

Any help would be appreciated.

Thanks in advance.

Attention !: Attached images Sample later and the signature in the screenshots was based on a Google search. I used it for demonstration purposes only.

+1
source share
3 answers

Since the size of the imageView is different from the original image, instead of merging two images using two ABSOLUTE positions, you can use the RELATIVE position.

Ship Code:

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

You can consider this 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; 
0
source

I needed to use imageView (A) frame.size instead of A.image.size to get the correct location when merging.

Here is 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 } 
0
source

All Articles