Jeeyaa
Jeeyaa

Reputation: 31

Erase the content inside the bezierPath

I’m working on a project that allows the user to draw anything on an image .. after the user finishes drawing . It closes the path and erase the content of image enclosed in a path and assign a new rectangular frame with a new image that can be dragged, but it only shows the content inside the erased path. Do you have any idea how can i achieve that ? I’m new to swift ios. and i have the code to erase the content of image.

What i need is :

  1. assign a new image in a resizable rectangular frame
  2. move the frame to resize accordingly so that it only shows the content inside the erased region This is what I’m trying to do

erase the content of image inside bezier Path

assign a new image in rectangular frame

the movable frame should only be visible inside the removed content of image

Upvotes: 1

Views: 68

Answers (1)

Anandh Krishnan
Anandh Krishnan

Reputation: 5984

Assuming you have your enclosed UIBezierPath

let path = // ... your enclosed UIBezierPath

Create a CAShapeLayer with the closed path

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath

Create an image layer

let imageLayer = CALayer()
imageLayer.contents = yourImage.cgImage

Apply the shape layer as a mask to the image layer

imageLayer.mask = shapeLayer

Assuming your view is drawingView

UIGraphicsBeginImageContextWithOptions(drawingView.bounds.size, false, 0.0)
drawingView.drawHierarchy(in: drawingView.bounds, afterScreenUpdates: true)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Upvotes: 0

Related Questions