I\'ve been able to get pretty far with what I\'ve been wanting to accomplish, and that\'s to replicate iOS\'s built in circular photo cropper for the built in contacts app.
In the code of Jakub Marek, there's an issue with persistent rounded layer if you open a second time the camera.
to solve it add in your openCamera func:
editLayer?.removeFromSuperlayer()
label?.removeFromSuperview()
and replace in private func hideDefaultEditOverlay(view: UIView)
subview.isHidden = true
by
subview.removeFromSuperview()
Code :
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
imagePicker.sourceType = UIImagePickerController.SourceType.camera
//If you dont want to edit the photo then you can set allowsEditing to false
imagePicker.allowsEditing = true
imagePicker.cameraDevice = .rear
imagePicker.showsCameraControls = true
imagePicker.cameraCaptureMode = .photo
imagePicker.delegate = self
editLayer?.removeFromSuperlayer()
label?.removeFromSuperview()
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: NSLocalizedString("Attention",comment:""), message: NSLocalizedString("You don't have any camera",comment:""), preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK",comment:""), style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
private func hideDefaultEditOverlay(view: UIView)
{
for subview in view.subviews
{
if let cropOverlay = NSClassFromString("PLCropOverlayCropView")
{
if subview.isKind(of: cropOverlay) {
subview.removeFromSuperview()
//subview.isHidden = true
break
}
else {
hideDefaultEditOverlay(view: subview)
}
}
}
}