Update: I fixed this : )
Hello my friends,
I am so close to figuring out the Milestone challange but running into 2 bugs that i just can't figure out :
-
After taking the photo, an alert window will pop up with textfield for me to add captions. However this alert window keeps popping out even after I have added the text, and will not go away unless i select "cancel".
-
I can't get the Image to show in my detailViewController. In the detailViewController, there is the Title, which should be the same as whatever captions i added to the photo previously, and then the image itself. The title is showing up fine, but nothing in the ImageView.
PS: The class i created is called " EachCell ", which contains var Caption, and var Photo.
My ViewController:
class ViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var pictures = [eachCell]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(takePhoto))
}
@objc func takePhoto() {
let picker = UIImagePickerController()
// picker.sourceType = .camera
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
/*Extract the image from the dictionary that is passed as a parameter.
Generate a unique filename for it.
Convert it to a JPEG, then write that JPEG to disk.
Dismiss the view controller.*/
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else{return}
let imageName = UUID().uuidString
let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
if let jpegData = image.jpegData(compressionQuality: 0.8){
try? jpegData.write(to: imagePath)
}
let newCell = eachCell(caption: "Caption: ", photo: imageName)
pictures.append(newCell)
tableView.reloadData()
dismiss(animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let rows = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CaptionedPhoto else{
fatalError("something went wrong with deque")
}
let person = pictures[indexPath.row]
let path = getDocumentsDirectory().appendingPathComponent(person.photo)
rows.imageView?.image = UIImage(contentsOfFile: path.path)
//either go here
// rows.caption.text = person.caption
let ac = UIAlertController(title: "Add captions to this photo", message: nil, preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "cancel", style: .cancel))
ac.addAction(UIAlertAction(title: "add", style: .default) {[weak self, weak ac] _ in
guard let newLabel = ac?.textFields?[0].text else{ return }
person.caption = newLabel
self?.tableView.reloadData()
}
)
present(ac, animated: true)
rows.caption.text = person.caption
return rows
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "detail") as? DetailViewController{
let person = pictures[indexPath.row]
vc.selectedImage = person.photo
vc.photoTitle = person.caption
navigationController?.pushViewController(vc, animated: true)
}
}
}
My DetailViewController:
class DetailViewController: UIViewController {
@IBOutlet var detailPhoto: UIImageView!
var selectedImage: String?
var photoTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
title = photoTitle
if let imageToLoad = selectedImage {
detailPhoto.image = UIImage(named: imageToLoad)
} else {
print("unable to pull image")
}
// Do any additional setup after loading the view.
}
Grateful for your help! Thank you all !