I would like to know if it is possible to save an image to realm database. If yes, how can I do that?
I understand Swift only!
Thank you in advance
The best way to save images and other kind of data in this case is to store only the URL to the file in the database. Attention: URL does NOT mean a remote URL.
For example, if you want to persist your image, you can create a folder in the Documents folder, and save any image there, with a random name or whatever you prefer.
Here's how to access the Documents folder using Swift:
let documentsPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsPath = documentsPaths.first
Then in your Realm, store only the path to the file ("/images/1R9RI8S.jpg"
).
That way, you can always retrieve your image file by fetching the Documents folder, plus your Realm is not bloated with large data, plus the content of the Documents folder is backed up by iOS.
You can store images as NSData
if images are small in size or less in numbers. However, if you have more images or large size store only paths.
Look at the related answer link