1

I want to know how I can save images I get from the bilbotrop of photos on the coredata.

I have an imageView that when touching it I can choose a photo from the library my problem is when I close the app and reopen the image is not there.

How can I save the image and when the app reopens the image continues in the imageview

Is coredata the best option for this?

class V1Menino: UIViewController, UITextFieldDelegate, GADBannerViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIScrollViewDelegate {

@IBOutlet weak var sViewV1: UIScrollView!
@IBOutlet weak var imageV1: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.layoutIfNeeded()

    self.sViewV1.minimumZoomScale = 1.0
    self.sViewV1.maximumZoomScale = 6.0

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    imageV1.isUserInteractionEnabled = true
    imageV1.addGestureRecognizer(tapGestureRecognizer)

}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.imageV1
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]){


    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        imageV1.image = image
    }
    self.dismiss(animated: true, completion: nil)
}

@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    //let tappedImage = tapGestureRecognizer.view as! UIImageView
    // sua acao
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    self.present(image,animated: true)
    {
        //depois disso ta completo
    }
}
}

2 Answers2

0

No, core data is not the best options in your case. Core Data is useful when you are dealing with relational schemes.
And anyway saving images directly in core data (even if it's possible) is not the best way to do that. Usually you save the image locally and just keep the path to it in core data persistent storage.
To do what you want is sufficient to create a directory in the document directory and save a copy of the image there.
Once you open the app just build a UIImage instance with the data at this path.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

I would not use CoreData for the image data. That should be stored somewhere in the Documents directory

You can use UIImageJPEGRepresentation to get Data from a UIImage and then save it with write(to:options:)

You can get the Documents directory with: How to find NSDocumentDirectory in swift?

I don't know how complex your app is. If you only need to store this one image, then make a subfolder under the Documents directory and use a constant file name.

If you need to store a bunch of images (and keep track of them), then you can use CoreData to just store the filenames (not the image data).

This tutorial does something like what you want (but with PNG): https://www.hackingwithswift.com/example-code/media/how-to-save-a-uiimage-to-a-file-using-uiimagepngrepresentation

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • my app is a work for a course it basically is a photo album. There are some views that you can add only one image as it has views that you can add 2 or more. What do you recommend I use? I just need to save this data to finalize my app. – Thiago Geisler Sep 25 '17 at 20:54
  • Save the images to the documents folder using names that you can figure out later "photo-1.jpg", "photo-2.jpg", etc. That's fine for a simple app for learning. In a more real one, the names would be stored somewhere (e.g. CoreData, a plist file, etc) – Lou Franco Sep 25 '17 at 23:45
  • Do I need to save the library photos with these names? How do I do this? Or are you telling me to put photos with these names inside the folder? I need to get the photos from the iphone library so the user can set up their own album. – Thiago Geisler Sep 26 '17 at 13:23
  • I don't really have enough information to give an exact recommendation. Generally, (1) you need to make up names for the files to save them in the write() functions (2) you need to remember those names somewhere to get the files back. So, if you have a limited list you could just use names like "photo-1.jpg", "photo-2.jpg", etc. You can pick any names you want, but you just need to get them back later. – Lou Franco Sep 26 '17 at 14:15