0

How to add an image in core data? The image type what that I have selected. And how to save the core data image. How can I add a image?

Scath
  • 3,777
  • 10
  • 29
  • 40
  • https://stackoverflow.com/a/27996685/8306054 Have you refered this.? – Abhirajsinh Thakore Apr 10 '18 at 11:32
  • 1
    And if your purpose is just to save the image. It would be good to save it in Document directory and store its path or name to core data so that you can fetch them – Abhirajsinh Thakore Apr 10 '18 at 11:34
  • i have save image and fetch the image in table view. –  Apr 10 '18 at 12:06
  • so u can use the code to save the image in document directory and save its path to core data and then show it in table view.From where are you getting images.? You are selecting that from image picker.? – Abhirajsinh Thakore Apr 10 '18 at 12:11
  • ok Thank you I will try it now –  Apr 10 '18 at 12:25
  • if u want i can make code for you. But please conform what is your image source.? I mean from image picker or any other.? – Abhirajsinh Thakore Apr 10 '18 at 12:27
  • i have use the below code is possible to save the image and data but image i have selected binary data. But it shows the no data to convert ui image, the below code i have tried CoreDataHandler.saveObject(fName: fname.text!, lName: lname.text!, mobilenumber: mobileNumber.text!, Age: age.text!, Dob: dob.text!,Image: image.UIImage) –  Apr 10 '18 at 12:34
  • You did not understand my question. My question is from where are you picking image to save.? Is that from Image picker or any other source like web server or any other.? – Abhirajsinh Thakore Apr 10 '18 at 12:36
  • i have taken the ui image view i have given the outlet connection of imagePicker i have given Kindly please arrage the code fo me. like the image picking from gallery or camera captures. –  Apr 10 '18 at 12:37
  • ok i got you point. Wait lemme arrange one for you. – Abhirajsinh Thakore Apr 10 '18 at 12:37
  • ok thank you for giving response to me –  Apr 10 '18 at 12:38
  • Please check the answer. If it works let me know – Abhirajsinh Thakore Apr 10 '18 at 12:44
  • its Working Thank You.. –  Apr 10 '18 at 13:29
  • This has been asked and answered many, many times on Stack Overflow. Please search for answers before posting a new question. – Tom Harrington Apr 10 '18 at 15:25
  • @BSuresh If it worked for you please approve my answer. – Abhirajsinh Thakore Apr 11 '18 at 04:45
  • hiii Could please help me on How to create the calendar in tableview above the list of tableview with calendar all month dates and everything.Please help me on that –  Apr 21 '18 at 05:20

1 Answers1

0

Step 1:- Save the image in Document Directory

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)
    let newPath = path.appendingPathComponent("image.jpg") //Possibly you Can pass the dynamic name here
    // Save this name in core Data using you code as a String.
    let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
    do {
        try jpgImageData!.write(to: newPath)
    } catch {
        print(error)
    }
}}

Step:-2: Fetch the Image back from the Document Directory and display index path row wise in table view cell.

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath          = paths.first
    {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("image.png") //Pass the image name fetched from core data here
        let image    = UIImage(contentsOfFile: imageURL.path)
        cell.imageView.image = image
}

This is simple and More convenient Method

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23