Reputation: 31
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 :
Upvotes: 1
Views: 68
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