1

How to add a circular cropper in UIImagePickerController when camera is running ? where picture should be cropped as per the circular area. (iPad)

class imagePickerHelper: NSObject {

    override init() {}

    var chosenImage: UIImage?
    var getimage: ((_ selectedItem: UIImage ,_ selectedvalue: Bool) -> Void)?
    static let helper = imagePickerHelper()
    var viewcontroller = UIViewController()

    func opencamera (withParentViewController ParentViewController: UIViewController,source: UIImagePickerControllerSourceType,selectedimage: @escaping (_ value: UIImage?,_ selectedvalue: Bool?) -> Void){
         if UIImagePickerController.isSourceTypeAvailable(.camera) {
             let picker = UIImagePickerController()
            viewcontroller = ParentViewController
            picker.delegate = self
            picker.sourceType = source
            picker.allowsEditing = true
            picker.sourceType = UIImagePickerControllerSourceType.camera
            picker.cameraCaptureMode = .photo
            picker.cameraDevice = UIImagePickerController.isCameraDeviceAvailable(.front) ? .front : .rear
            picker.modalPresentationStyle = .fullScreen
            viewcontroller.present(picker,animated: true,completion: nil)
            getimage = selectedimage
        }
        else{
            noCamera()
        }

    }

    func noCamera() {
        let alertVC = UIAlertController(
            title: "No Camera",
            message: "Sorry, this device has no camera",
            preferredStyle: .alert)
        let okAction = UIAlertAction(
            title: "OK",
            style: .default,
            handler: nil)
        alertVC.addAction(okAction)
        viewcontroller.present(
            alertVC,
            animated: true,
            completion: nil)
    }
}

extension imagePickerHelper: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    //MARK: - Delegates
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage //2
        // myImageView.contentMode = .scaleAspectFit //3
        // myImageView.image = chosenImage //4
        getimage!(chosenImage!,true)
        viewcontroller.dismiss(animated: true, completion: nil) //5
        picker.dismiss(animated: true, completion: nil);
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        viewcontroller.dismiss(animated: true, completion: nil)
    }

}
Scriptable
  • 19,402
  • 5
  • 56
  • 72
Subhajit
  • 11
  • 1
  • My first thought is creating a custom camera view and masking the camera layer with a circular CALayer. – Tamás Sengel Apr 04 '18 at 11:01
  • You can override the overlay view to provide a circular overlay. Then to actually crop in a circular way you would have to take the cropRect value from the provided dictionary and then crop it yourself – Scriptable Apr 04 '18 at 11:02
  • yes creating a custom camera view is a option but I am trying to do it using UIImagePickerController @the4kman – Subhajit Apr 04 '18 at 11:07
  • how can I provide a circular view in camera overlay view and capture taken picture of only that area? @Scriptable – Subhajit Apr 04 '18 at 11:09
  • @Subhajit providing the view should be fairly easy, you use the build in editor to get a crop rectangle and THEN you crop into a circle. its usually best to keep the rectangle and just display it as a circle by rounding the corners. then if your requirements change you still have the original image – Scriptable Apr 04 '18 at 11:18
  • @Scriptable Thank you for your feedback. but build in editor comes after taking the picture that is not the requirement. I want to open the camera with round area where user should place there face and take the picture. – Subhajit Apr 04 '18 at 11:34
  • @Subhajit I have done this in the last few weeks, you are not understanding what I am saying. YOU provide the camera with the overlay. your view/overlay that you provide has a circular window in it. the camera takes a rectangular/square picture no matter what you do. YOU have to take the rectanglular/square picture from the camera and make it round manually. What the user sees in the camera preview window is what the eventual output will be, but the camera doesnt do it all for you. Its too much to explain here, I'll see if I can find an example for you – Scriptable Apr 04 '18 at 11:44
  • see [cameraOverlayView](https://developer.apple.com/documentation/uikit/uiimagepickercontroller/1619113-cameraoverlayview) and https://stackoverflow.com/questions/28365819/ios-custom-uiimagepickercontroller-camera-crop-to-circle-in-preview-view – Scriptable Apr 04 '18 at 11:57
  • @Scriptable now I can understand. any example will help me a lot. thank you – Subhajit Apr 04 '18 at 12:00

0 Answers0