2

I'm creating an app to capture photos and save it along with some UITextField data.

I've used core data to store UITextField data and works fine but I can't save images using core data.

I have captured photo and segued that photo to next view controller for preview and from there I need to save and segue to home view controller to display the pic with relevant UITextField data in UITableViewCell.

Everything works fine for UITextField to save and fetch in UITableViewCell but unable to save and fetch images.

Mr.Kushwaha
  • 491
  • 5
  • 14
Dixith Maram
  • 33
  • 1
  • 3
  • 12
  • Im unable to save images in core data just like textfields – Dixith Maram May 03 '18 at 11:01
  • You wantto save images in document directory and fetch the same on another view controller right? – Vinaykrishnan May 03 '18 at 11:03
  • yes exactly but the images are captured using camera in different view controller and its preview in different view controller – Dixith Maram May 03 '18 at 11:07
  • Create a class with to save and load images method, and you can call those method in respective classes. Is it that easy – Vinaykrishnan May 03 '18 at 11:08
  • you can convert image to data and save in to coredata and fetch it back,other way is save to document directory and you can fetch it where ever its required. this will help you https://stackoverflow.com/questions/6821517/save-an-image-to-application-documents-folder-from-uiview-on-ios – Satheeshkumar Naidu May 03 '18 at 11:10
  • can you share some code to save and load in document directory – Dixith Maram May 03 '18 at 11:18
  • https://stackoverflow.com/a/36221404/1142743 this helps you save image in document directory and path in coredata – Vinodh May 03 '18 at 11:22

2 Answers2

8

In case you are still struggling to figure out here is the code snippet, good luck

func saveImageToDocumentDirectory(image: UIImage ) {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let fileName = "image001.png" // name of the image to be saved
    let fileURL = documentsDirectory.appendingPathComponent(fileName)
    if let data = UIImageJPEGRepresentation(image, 1.0),!FileManager.default.fileExists(atPath: fileURL.path){
        do {
            try data.write(to: fileURL)
            print("file saved")
        } catch {
            print("error saving file:", error)
        }
    }
}


func loadImageFromDocumentDirectory(nameOfImage : String) -> UIImage {
    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(nameOfImage)
        let image    = UIImage(contentsOfFile: imageURL.path)
        return image!
    }
    return UIImage.init(named: "default.png")!
}
Vinaykrishnan
  • 740
  • 3
  • 14
  • 34
  • can i call load function in tableview cell? – Dixith Maram May 03 '18 at 11:59
  • Since you are loading images from document dierctory and not calling any api to laod image from server. You can go with it. – Vinaykrishnan May 03 '18 at 12:02
  • i didnt create any imageName seperately, which image name do i need to pass while calling function loading in tableview? – Dixith Maram May 07 '18 at 05:46
  • Image name is a unique identifier for the image, while storing you are required to specify the image name "fileName",so it would be easy to fetch that particular image using the same name – Vinaykrishnan May 07 '18 at 06:03
  • so do i need to call that load function in tableview cell pass nameOfImage parameter with fileName value? – Dixith Maram May 07 '18 at 09:29
  • Filename is the string that is used to represent the image right? you don't need to send another parameter then – Vinaykrishnan May 07 '18 at 09:32
  • Im asking which string do i need to pass in nameOfImage while loading it in tableViewCell – Dixith Maram May 07 '18 at 09:35
  • While saving the image in "func saveImageToDocumentDirectory(image: UIImage )" you might have given some filename right? For example : let fileName = "image001.png" // name of the image to be saved – Vinaykrishnan May 07 '18 at 09:36
  • im taking pictures through camera when i used that function in tableViewCell it is displaying the same image for every cell – Dixith Maram May 07 '18 at 09:39
  • Did you ever get this working? I am also having trouble taking and loading a picture. – Drewgost Nov 21 '20 at 20:45
0
  import UIKit
  class ViewController: UIViewController { 
    @IBOutlet weak var myimageview: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()   
        getSaveImage()
    }

func getSaveImage()
{
    let document = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            
            print("Document :\(document)")
            
            let imgurl = document.appendingPathComponent("img1", isDirectory: true)
           
             print("Image Url : \(imgurl.path)")
            
            if !FileManager.default.fileExists(atPath: imgurl.path)
            {
                do{
                    try UIImage(named: "img1")!.jpegData(compressionQuality: 0.75)!.write(to: imgurl)
                    print("image save")
                }
                catch{
                    print("image not save")
                }
            }
            myimageview.image = UIImage(contentsOfFile: imgurl.path)
        }
   }