I've looked everywhere but can't seem to find a suitable solution for saving and using a photo. I am building a basic application where a user has to select an image that later would be added to an array of UIImage object. This application consist of a table view consisting of cities that the user has been and a description. The user has the ability to add a new city, description, and image. When adding a new image I went and did a 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)
}
Through the segue I transfer all the information to the another view control, but when selecting a photo that information doesn't transfer over. I would like to know how to add the photo selected be added to the UIImage array.
I could upload all my code if you wish but it is a lot. I know this seems confusing so if you have any questions please ask. Thank you!