UIImagePickerController中的循环裁剪

时间:2018-04-04 10:57:51

标签: ios swift ipad camera uiimagepickercontroller

如何在相机运行时在UIImagePickerController中添加循环裁剪器?应根据圆形区域裁剪图片。总览

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)
    }

}

0 个答案:

没有答案