ios - 从 subview 启动时 ImagePickerController 立即被解雇

标签 ios swift presentmodalviewcontroller

我正在尝试以模态方式呈现 ImagePickerController。我触发当前操作的按钮位于主视图的 subview 内。 按下按钮时,ImagePicker View 将立即显示并关闭。 我对主视图中的另一个按钮进行了相同的尝试,并且工作正常。

我的按钮操作代码:

@IBAction func pressUploadProfilePhoto(_ sender: Any) {
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil)
}

类减速度:

class ProfileVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self            
    }
}

委托(delegate)方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
    profilePicImg.contentMode = .scaleAspectFill
        profilePicImg.image = pickedImage
    }
    self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
      dismiss(animated: true, completion: nil)
}

我的 View 层次结构:

enter image description here

我错过了什么?

最佳答案

Please Try the below code, may be your problem get resolved ->

1) Firstly add these Delegates in your Class.

class XYZ: UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate

2) Create a variable ->
let imagePicker = UIImagePickerController()

3) Set imagePicker delegate in viewDidLoad
override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
}

4) Write Delegate Methods for UIImagePicker

// MARK: - ImageView Controller Delegate Methods

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageviewProfile.contentMode = .scaleToFill
            imageviewProfile.image = pickedImage

        }
        dismiss(animated: true, completion: nil)
    }

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

5) Now your Action Method ->

@IBAction func pressUploadProfilePhoto(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicker.allowsEditing = false
                imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
                imagePicker.modalPresentationStyle = .fullScreen
                present(imagePicker, animated: true, completion: nil)

            }
}

关于ios - 从 subview 启动时 ImagePickerController 立即被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670729/

相关文章:

ios - 当前模态视图 Controller 完成滑动后如何制作动画?

ios - UITabBarController Tab Modal Presentation -Swift2 OS

ios - 调用presentModalView后ViewController viewDidLoad?

ios - 我如何使用 Braintree 保存客户卡信息,以便稍后他们购物时我可以从中扣款?

ios - 如何创建具有适当不透明度的 UITextField 附件 View ,甚至复制键?

ios - 如何在 iOS 中全局更改字体大小

ios - 在 Action 中更改同一 Tableview 单元格中的不同按钮(Swift)

ios - swift 3.0 中的 Uibutton 问题

c# - 是否可以在 MonoTouch 中访问 NSURLCache?

ios - 如何调整导航栏标题和编辑后退按钮