gpt4 book ai didi

ios - UIImagePickerController swift 的问题

转载 作者:行者123 更新时间:2023-11-30 11:09:52 31 4
gpt4 key购买 nike

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

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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com