0

I am trying to crop my image using UIBezierPath (the user draw the path) I want the path area to be removed from the UIImage, I have this code implementation but the cut is not working in the path area

    let originalImage = treaCameraImage!
    
    UIGraphicsBeginImageContext(originalImage.size)
    originalImage.draw(at: .zero)
    
    let context = UIGraphicsGetCurrentContext()
    context!.addPath(path.cgPath)
    context!.clip()
    context!.setFillColor(UIColor.red.cgColor)
    context!.clear(CGRect(x: 0, y: 0, width: originalImage.size.width, height: originalImage.size.width))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    imageCut.image = newImage

image1 image2

mariovzc
  • 173
  • 1
  • 2
  • 12
  • 2
    Because you are not taking account of the fact that the image as displayed in an image view is not the same as the actual image (`originalImage`). You need to _map_ the path from the image _view_ image to the _actual_ image so that it is the right size (larger) and in the right place. – matt Aug 28 '22 at 16:24
  • 1
    Do you want to create a ***new*** `UIImage` with a transparent "hole"? Or, do you want to display the image in a `UIImageView` and "cut a hole" in it? – DonMag Aug 28 '22 at 16:24
  • @DonMag i want a new image – mariovzc Aug 28 '22 at 16:25
  • @matt can you explain more? because I don't get it – mariovzc Aug 28 '22 at 16:26
  • You have not given anywhere _near_ enough information — like where you got the path from. What, at least, is the `contentMode` of the image view in which the user drew the path? – matt Aug 28 '22 at 16:27
  • @matt the user Draw the path and the contentmode is Aspect Fit – mariovzc Aug 28 '22 at 16:39
  • 2
    Here's the solution for aspect fill; you can easily adapt it for aspect fit. https://stackoverflow.com/questions/43720720/how-to-crop-a-uiimageview-to-a-new-uiimage-in-aspect-fill-mode/43720791#43720791 – matt Aug 28 '22 at 16:43
  • @mariovzc - did you get this worked out? or are you still looking for a solution? – DonMag Aug 29 '22 at 20:24
  • @DonMag i already have to solution but I am going to add the solution later. – mariovzc Aug 30 '22 at 20:09

1 Answers1

0

Well for the solution you need first add this code in the viewDidAppear

 override func viewDidAppear(_ animated: Bool) {
    //Implemented to overcome trackerview problem(UIBreizerPath is not correct) when working direcly on an image bigger than the screen(For brackets cut image).
    if (self.treaCameraImage!.size.width != self.view.bounds.size.width || self.treaCameraImage!.size.height != self.view.bounds.size.width) {
        
        UIGraphicsBeginImageContext(self.imageCut.bounds.size);
       
        let context = UIGraphicsGetCurrentContext()
        if let context = context {
            imageCut.layer.render(in: context)
        }
        let screenShot = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext();
        self.treaCameraImage = screenShot;
        //self.tempOriginalImage = screenShot;
    }
}

The treaCameraImage is the default image and imageCut is the image container

In my method to cut the image i changed:

context!.clear(CGRect(x: 0, y: 0, width: originalImage.size.width, height: originalImage.size.width))

to this:

context!.clear(CGRect(x: 0, y: 0, width: originalImage.size.width * 2, height: originalImage.size.width * 2))

and that works for me! enter image description here

mariovzc
  • 173
  • 1
  • 2
  • 12