I have a “Choose Button” button in my view controller then when pressed, access photo picker. Though I can press on the photo I want, It just goes back to the view controller with the “choose button” instead of replacing the button with the photo chosen.
Here is the code I use for the View Controller
import UIKit
class StitchDetailViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var summary: Summary?
@IBOutlet var choosePhotoButton: UIButton!
@IBOutlet weak var notesTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
updateView()
}
func updateView() {
guard let summary = summary else {return}
if let imageData = summary.imageData,
let image = UIImage(data: imageData) {
choosePhotoButton.setTitle("", for: .normal)
choosePhotoButton.setBackgroundImage(image, for: .normal)
} else {
choosePhotoButton.setTitle("Choose Image", for: .normal)
choosePhotoButton.setBackgroundImage(nil, for: .normal)
}
notesTextField.text = summary.notes
}
@IBAction func choosePhotoButtonTapped(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { action in
imagePicker.sourceType = .camera
self.present(imagePicker, animated: true, completion: nil)
})
alertController.addAction(cameraAction)
}
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { action in
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
})
alertController.addAction(photoLibraryAction)
}
present(alertController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.originalImage] as? UIImage else {
return
}
summary?.imageData = image.pngData()
dismiss(animated: true, completion: nil)
updateView()
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
@IBAction func saveButtonPressedtwo(_ sender: Any) {
guard let notes = notesTextField.text,
let imageData = choosePhotoButton.imageView else {return}
summary = Summary(notes: notes, imageData: nil)
performSegue(withIdentifier: "StitchList", sender: self)
}
}
I tried adding a breakpoint after the updateView() is called again and it crashed so I know the problem is in that chunk of code, but I don’t know what code to add, please help! I’m still a novice coder and the code I’m using after my choosePhotoButtonTapped is borrowed from another app I’ve worked on and it works perfectly fine in the other app..
meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.