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.
- I have written all the code for drag and drop.
- I couldn't find a proper position of B for merging with A.
Here are the problem screenshots:
Showing the initial position of B.
This is the new position of B after drag and drop.
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
}