ios - How to crop UIImage with UIBeziePath with all scale factors? -
i'm trying crop uiimage given uibezierpath i've drawn in view.
code:
// "self.imageedited" initial image needs cropped let path = .... //my uibezierpath let layer = cashapelayer() layer.path = path.cgpath self.imagetoeditview.layer.mask = layer uigraphicsbeginimagecontextwithoptions(self.imageedited.size, false, 1.0) // <-- why it's not working 0.0 ? self.imagetoeditview.layer.render(in: uigraphicsgetcurrentcontext()!) let image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() //here "image" cropped haves white background let croppedcgimage = image?.cgimage?.cropping(to: path.cgpath.boundingbox) let croppedimage = uiimage(cgimage: croppedcgimage!) self.imagetoeditview.layer.mask = nil uiimagewritetosavedphotosalbum(croppedimage, nil, nil, nil)
this code works, crops image desired. if change scale factor of uigraphicsbeginimagecontextwithoptions 0.0 (to default scale of device, create same "image" when runs this:
let croppedcgimage = image?.cgimage?.cropping(to: path.cgpath.boundingbox)
i not right image cropped. crops different portion.
should scale path?
Comments
Post a Comment