0

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!

Mark Young
  • 39
  • 5
  • I keep seeing nil and getting ***Fatal error: Index out of range*** when trying to get the image. I've looked everywhere and tried everything – Mark Young Oct 11 '19 at 22:39

2 Answers2

0

You must conform to the delegate but you just add conditional cast and it never works.

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
    }
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

step 1 [ very important ] : you have to set permitions in your info.plist

like this image

the key is Privacy - Photo Library Usage Description in info.plist

step 2 : your view controller should confirm these protocols UIImagePickerControllerDelegate , UINavigationControllerDelegate
and then you can modify what you want here is a smiple example of that

 class MyVC: UIViewController , UIImagePickerControllerDelegate , 
UINavigationControllerDelegate {

let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
     imagePicker.delegate = self
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .photoLibrary
}


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

        self.myImageView.image = pickedImage //the image of your image view should be changed here 


    dismiss(animated: true, completion: nil)
}

@IBAction func showGallery(_ sender: UIButton) (){
// the function for showing images from gallery
self.present(imagePicker, animated: true, completion: nil)
}

}
AzAli
  • 1
  • 3