ios - UIImagePickerController swift 的问题

标签 ios swift

我正在开发一个需要相机的应用程序。我打开的方式是这样的:

let Picker = UIImagePickerController()
Picker.delegate = self
Picker.sourceType = .camera
present(Picker, animated: true, completion: nil)

一切都很完美,这是我得到的结果:

我现在想做的是将“取消”按钮替换为一个让用户从他的照片库中选择照片的按钮。我已经阅读了很多,但仍然没有得到解决方案。

谢谢大家

最佳答案

你可以这样做而不是那样

    @IBAction func buttonOnClick(_ sender: UIButton)
{
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallary()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    /*If you want work actionsheet on ipad
    then you have to use popoverPresentationController to present the actionsheet,
    otherwise app will crash on iPad */
    switch UIDevice.current.userInterfaceIdiom {
    case .pad:
        alert.popoverPresentationController?.sourceView = sender
        alert.popoverPresentationController?.sourceRect = sender.bounds
        alert.popoverPresentationController?.permittedArrowDirections = .up
    default:
        break
    }

    self.present(alert, animated: true, completion: nil)
}

func openCamera()
{
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallary()
{
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imagePicker.allowsEditing = true
    self.present(imagePicker, animated: true, completion: nil)
}

关于ios - UIImagePickerController swift 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52294669/

相关文章:

ios - 在模拟内存警告期间销毁 View

ios - 在 swift ios 中使用 webkitview

swift - 文本与 TextView swift 中的自定义按钮重叠

ios - 无法将类型 '(_) -> ()' 的值转换为预期的参数类型 '((Bool, Error?) -> Void)?'

swift - 如何在 Swift 单元测试中断言可选 Bool 的值?

swift - 如何用swift编写汇编

ios - 无法使用 AWSMobileClient iOS 登录。获取 AWSMobileClientError 错误 13

ios - 我应该如何与契约(Contract)开发人员一起管理 iOS 证书和 key ?

ios - 如何在特定图像旋转时触发事件?

ios - 滚动时电子节目指南 View 如何更新?