gpt4 book ai didi

arrays - 用户按下按钮(保存)后如何从照片库中打开并选择照片

转载 作者:行者123 更新时间:2023-11-30 10:34:38 25 4
gpt4 key购买 nike

我到处寻找,但似乎找不到合适的解决方案来保存和使用照片。我正在构建一个基本应用程序,用户必须选择一个图像,稍后将其添加到 UIImage 对象的数组中。该应用程序由一个 TableView 组成,其中包含用户去过的城市和描述。用户可以添加新的城市、描述和图像。添加新图像时,我做了一个alertController

@IBAction func addCity(_ sender: AnyObject) {
let alertController = UIAlertController(title: "Add new city", message: "", preferredStyle: UIAlertController.Style.alert)

alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter City Name"
}
let saveAction = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
let secondTextField = alertController.textFields![1] as UITextField

self.newCity = firstTextField.text!
self.city.addCity(city: self.newCity)

self.newDescription = secondTextField.text!
self.fruitTable.reloadData()

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary
){
print("Button capture")

var imagePicker = UIImagePickerController()
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary;
imagePicker.allowsEditing = false

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

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in

})

self.newImage = image
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Description"
}

alertController.addAction(saveAction)
alertController.addAction(cancelAction)

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

通过segue,我将所有信息传输到另一个 View 控件,但是当选择照片时,该信息不会传输。我想知道如何将选定的照片添加到 UIImage 数组中。

如果您愿意,我可以上传我的所有代码,但数量太多了。我知道这看起来很困惑,所以如果您有任何疑问,请询问。谢谢!

最佳答案

您必须符合委托(delegate),但您只是添加条件转换,但它永远不会起作用。

class MyButtonContainerViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {

@IBAction func addCity(_ sender: AnyObject) {
,,,
imagePicker.delegate = self // No casting needed
,,,
}

// This is the delegate function and must be in the root of the `Class` (not nested in the `addCity` function)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picket.dismiss(animated: true, completion: nil)
let image = info[.originalImage] as! UIImage
self.newImage = image
// Append image to the array here
}
}

关于arrays - 用户按下按钮(保存)后如何从照片库中打开并选择照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58346639/

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