1

I have an entity named Item. I have an export function that exports all Items to CSV. So name, weight, quantity and etc is all exported correctly. The purpose of this is to save the data so that it may be imported later if all the data was deleted. One of the attributes of Items is a picture that the user chooses from its own library. How do I export that picture, so that it can be reimported later?

This is on iOS using the latest swift and Xcode.

I know I have not included any code, I am mainly asking for a direction to look. I'm not sure if I can get the location of the image on the device and then save that to the CSV or if there's a similar way. Thank you!

udi
  • 3,672
  • 2
  • 12
  • 33
  • Is the image stored in Core Data? – Joakim Danielson Jan 31 '22 at 08:00
  • Yes it is. But when I’m looping through the items, it’s converting everything to text which you can’t do with an image. Is there a way to get the location of that image so that I can store that as a string? – The Gold Spartan Jan 31 '22 at 13:29
  • If you store it in Core Data then you don't have a location (path to file on disk). Maybe better to store the image outside of Core Data and then only store a path/url in core data, this would solve the issue with CSV. – Joakim Danielson Jan 31 '22 at 13:44
  • So the app itself is storing someones entire collection of items. So it would be a lot of pictures. Where else would be a good idea to save them? – The Gold Spartan Jan 31 '22 at 15:37
  • If you store them outside Core Data like in the Documents folder or Application Support folder then you wouldn't need to create a copy of them when exporting to the CSV file. Or you don't copy the image at all but stores a url to the image in Photo Library. You should be able to find questions regarding both of these ways here at SO – Joakim Danielson Jan 31 '22 at 16:06
  • Thank you! Can you give me a hint at what to search for? I searched for saving images to documents swiftui and nothing pulled up. A lot of what comes up is saving to someones camera rolll which isn't what I need. – The Gold Spartan Jan 31 '22 at 22:33

3 Answers3

0

CSV is a text based file format but Images are binary data. So the two does not mix well.

One thing you can do is convert the image to Base64 String and insert that string to the CSV. But the string would be too large and there may be consequences.

If you are using the same device (probably not) you can get the path of the image and append it to the CSV.

If you are using a DB simply upload your images to it and add the path to the CSV. (you can even upload the images to your drive and add the path)

There may be other ways also.

udi
  • 3,672
  • 2
  • 12
  • 33
  • So the app itself is storing the users entire collection of items. Each item has 2 pictures at this moment. The only place I am storing them is core data and one of the reasons for that is the collection is something that people are going to want privacy on. So it doesn't store their information anywhere but on the device itself. Can you think of a place to store them on the device that would give me a location on the device? – The Gold Spartan Jan 31 '22 at 15:39
  • @TheGoldSpartan check the answers in this [post](https://stackoverflow.com/questions/29434617/ios-best-practice-to-save-images-locally-nscache-vs-save-in-document-directo) and this [post](https://stackoverflow.com/questions/37344822/saving-image-and-then-loading-it-in-swift-ios). This might help. – udi Feb 01 '22 at 05:42
0

So I solved this problem using the code below but I may have created a new one. I'll be posting a new question to better clarify

The code below allowed me to save to Documents which allowed me to export and import the images.

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

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

I think instead of storing the image as a string64, it might be better to find a way to convert the image to a web url, and you put that in the cells.