Save image into realm [duplicate]

筅森魡賤 提交于 2020-02-05 08:28:00

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/33763044/save-image-into-realm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!