I’m using a a bezier path to draw any shape on Image view. When the touch ends , i’ve closed the path to get the shape .. now i want to achieve the blank area that is inside the bezier path so that i can replace it with another image later on. Rest of the image remains same.
By "Replace with another image " I mean ,I want to assign a new image to the cropped portion or the black hole whatever,.. The result will be like a sticker over an imageView with the path drawn by the user.
I want to crop the content inside the closed path so that when I assign a new image to the path, it’ll resize and adjust into the closed path. Please do not use masking in the new assigned image. I’ll have to create a method to resize it’s frame, but that too inside the bezierpath. I am attaching the image of desired result too.
The background image
When path ends a new image gets assigned to the closed path with the exact frame as closed path.
2nd grass image gets assigned to path and adjusts its frame
The frame can then be resized by the user. But the grass image shouldn’t move outside the path.
3rd resizable frame of grass image inside path
I hope I’m making sense here.
2
Answers
We’re missing a lot of detail about your ultimate goal, but this may get you on your way…
Instead of trying to "cut a hole and fill it" we can:
So, the view hierarchy looks like this (with no mask set on the grass):
when we set a path for the grass layer mask, it looks like this:
When running and the user is drawing:
The user stops drawing, we close the path, and apply it to the grass view:
Sample code to see that in action…
//
FillImageView
class – this will hold the "grass" image, and will apply a layer mask://
DrawImageView
class – this will hold the "street" image, and handle drawing the dashed line://
DrawMaskView
class – manages theDrawImageView
and theFillImageView
:// simple example view controller – uses images named "street" and "grass":
To achieve this, you can follow these steps:
Create a Mask Image from Bezier Path:
After the user completes drawing the path, you can create a mask image from the Bezier path. This mask image will have a transparent area inside the path and opaque outside.
Apply the Mask to the Original Image:
Apply the mask image to the original image so that you have the content of the original image inside the user-drawn path and the rest outside the path.
Overlay the Result with Another Image:
Now you can overlay the maskedImage with another image (your sticker) to achieve the desired effect.
Here are the helper extensions and methods you can use: