0

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)


}
tsh79
  • 69
  • 4

1 Answers1

0

Yes you can use CoreData and create a Database to store the images, then retrieve them. But maybe that is a lot of hassle if you are only going to store 3 images at most, and then maybe change them, but always a maximum of 3.

Maybe a simpler solution would be to save the images to the Documents Directory, with a specific name like imageButton1.png and retrieve them or replace them whenever necessary.

Here a post on how to do it:

Save An Image To Application Documents Folder From UIView On IOS

vicegax
  • 4,709
  • 28
  • 37
  • thanks zero, but that answer seems to be largely Objective-C based. I think I need something a bit more basic to follow from a Swift 3 perspective. – tsh79 Aug 05 '17 at 16:17
  • Not really, in that same post there is a Swift version of the answer: https://stackoverflow.com/a/39062752/4691224 – vicegax Aug 05 '17 at 16:21
  • Yes, saw that, unfortunately still beyond my current level of comprehension. – tsh79 Aug 05 '17 at 16:30
  • So I have just discovered that adding: UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) to my imagepickercontroller will save any camera images to the photo library. Perhaps a way to do this is to just rely on the images stored in the library and reference those ? – tsh79 Aug 05 '17 at 17:05
  • Going through the usual cycle of reading stackoverflow, add code, delete code, repeat. Is saving images really this complicated in iOS ? crazy ! – tsh79 Aug 05 '17 at 19:26
  • Is not that complicated, really. Don't save to Photos Album, it will be easier if you save to "Documents Directory". There are many tutorials online, here another post on the topic: https://stackoverflow.com/a/44646672/4691224 – vicegax Aug 07 '17 at 12:58