I have multiple buttons on the screen.
When you click a button it calls a function that uses UIImagePickerController to choose an image from either the Library or the Camera and saves that image to the button icon. (so basically clickable pictures).
I am now trying to add some persistence so that when the app is restarted the buttons save their assigned images. I have yet to find an answer I can follow that explains how to achieve this.
I have added the Core Data framework to my project. Can anyone advise on how to proceed. - Thanks.
var newPicImg : UIButton?;
@IBOutlet weak var ButtImg1: UIButton!
@IBOutlet weak var ButtImg2: UIButton!
@IBOutlet weak var ButtImg3: UIButton!
Buttons:
@IBAction func chooseImage(_ sender: UIButton) {
newPicImg = ButtImg1
picStuff()
}
@IBAction func chooseImage1(_ sender: UIButton) {
newPicImg = ButtImg2
picStuff()
}
@IBAction func chooseImage2(_ sender: UIButton) {
newPicImg = ButtImg3
picStuff()
}
Take/Choose Picture:
func picStuff()
{
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action:UIAlertAction)in
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action:UIAlertAction)in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil ))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
newPicImg?.setImage(image, for: .normal)
picker.dismiss(animated: true, completion: nil)
}