Home > front end >  Save Image (SwiftUI) to Realm database
Save Image (SwiftUI) to Realm database

Time:12-14

I am using SwiftUI and Realm.

I want to save an Image to the database. The image is a photo taken from the device's camera and is not stored in the gallery.

It's my understanding that I need to convert the image to NSData, but I can only find syntax that works with UIImage.

I tried

let uiImage: UIImage = image.asUIImage()

but get this error:

Value of type 'Image?' has no member 'asUIImage'

What am I doing wrong here, how can I have an Image to Realm local database?

EDIT: Thanks for the suggested "possible duplicate", but no: the duplicate (which I have already seen prior to making my post) is for UIImage (UIKit). I am using Image (SwiftUI).

CodePudding user response:

The struct Image is only a building block for the UI in Swift UI and it is not an object that represents the literal image, but rather something that displays some image.

The common approach, is to see how you create Image - where do you get the actual image from - and to use the source, the image itself to save it.

Just as side note, I wanted to mention that storing data blobs in Realm database can be extremely slow and more commonly used and fast approach is to write files to disk and to store only the names of those files in the database.

  • Related