0

I am making an app where you can post an image and display it on another page. I want to save it to a database but I don't know the best way to do it.

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36

3 Answers3

1

you can save your image in document folder by using

func saveImage(image: UIImage) -> Bool {
    guard let data = UIImageJPEGRepresentation(image, 1) ?? UIImagePNGRepresentation(image) else {
        return false
    }
    guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
        return false
    }
    do {
        try data.write(to: directory.appendingPathComponent("fileName.png")!)
        return true
    } catch {   
        print(error.localizedDescription)
        return false
    }
}

and you can get it your image with this method :

func getSavedImage(named: String) -> UIImage? {
    if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
        return UIImage(contentsOfFile: URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(named).path)
    }
    return nil
}
0

You can use Core data in swift to save Image. Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. By using Core data you can access sqlite database easily.

To save image in Sqlite Using Core Data See saving-picked-image-to-coredata

Azizul Hoq
  • 589
  • 1
  • 4
  • 13
0

you save your image in device and save path of that image in Sqlite.

this link for how to save image in device Saving image and then loading it in Swift (iOS)

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36