So I need to save pictures (made by the user) inside the app. I don't want to send them to a server and keep the link.
I am currently using Realm to save data, but I heard that it was not ideal to save images. What are the best practices for saving images locally?
(I will sometimes need to save hundreds of images.)
What kind of your data do you save ? ( link, data, image).
You should convert image to base64 then you save them into Realm. let write them in background task.
it will reduce volume and time to store them in Realm.
Related
I am trying to upload images to firebase along with other string and double data type.
I am think about 2 options now, one is uploading the imageUrl to firebase Realtime Database and when I retrieved it, I will retrieve a imageUrl and convert it to image.
Another way is to upload the image file to Firebase storage.
Which one is the preferred one to do? From the speed of loading image? or other factors that I haven't thought of.
Thank you for your help! I appreciate it.
Don't store large binary objects in Realtime Database. Use Cloud Storage instead.
Upload image urls is a better option.
Upload the image is a heavier task, hence it is better to upload urls with other data.
It depends on how big your app is going to scale. However, storing a large binary files in real time database is not a good practise. I would suggest you to use cloud storage, usually AWS buckets to store images and save the URL to firebase, to retrieve it later.
Storage is given from firebase to store all your large data and then fetch asynchronously. So using storage should be a better way rather than database.
For any kind of file you must have to use storage, and then store only file name in your realtime database to maintain good performance of your application. And that is the main reason firebase provide those functionalities in separate ways.
How can I make the way I add images to the core data more efficient?
Its a pretty bad idea to save "Data" or Image here in core data persistance. Also i think you are running this code in the background queue, if not then thats also a bad thing. But then again, saving the image or data into core data persistant store is a very bad idea and should be avoided whenever you can.
As an alternate you can do this -
Save the image in the local directory with a path and a unique
filename.
Save the filename in core data except the path.
Next time when you retrieve the image, get the filename from the Data store.
Append the filename with the whole path untill the folder. Retrieve the image.
This is a much more efficient and better way to store images.
I am working on a project where i want to make the data, text, images available in offline mode as well.
I fetch data from a web-service which includes image urls and other data. I store the text data in core data entities, however i don't save images locally but fetch them in realtime.
To view images in offline mode i will have to save them to local storage. However i am wondering if it would be the right approach. Saving images to local may possibly eat up a lot of storage on user's device.
What is the best approach to address this problem?
Should i save images to local or should i fetch them on run time only?
Use NSCache. With NSCache you can set a limit to how many images and so on are cached. See Apple's documentation for more details: https://developer.apple.com/reference/foundation/nscache?language=objc
Edit:
Never mind NSCache, just save the images as files. NSCache can still save you network usage and allow your app to be more responsive, but it is not what you want.
Should i save images to local or should i fetch them on run time only?
This question for you , you should decide what you need or what is will be more suitable for your app . at any way if you want to cache images i suggest to use this library SDWebImage
My app is a mood diary and to save data I've chosen to use Core Data (for strings, images, etc.); to allow the user to restore his diary I've implemented iCloud, that works well with Core Data.
Everything works well if I have not much entries, but when the images saved are too much the app is slow to load data and encounters memory warnings.
To save my images I've chosen Transformable data type and not Binary Data; I know that is not the best way to save images (saving url is surely better), but I need to sync my data on iCloud, and saving images as Transformable allows me to sync data in a simple way (thanks to the possibility offered by Apple Api to link Core Data and iCloud).
What can I do to avoid this memory warnings and sync my app pics on iCloud?
I've considered the possibility to save my pics on a custom photo album (if iCloud is activated for Photo app my app pics would be synchronized), but I need to save them with custom name to retrieve them from camera roll to my app, and for the moment I don't find any solution to save pic with custom name in a custom photo album.
Saving photos in document directory (and saving urls in my core data entity) would be the right choice for local database, but my app pics would not be synchronized.
There are a few things you can try.
First, add an image thumbnail property which stores a smaller version of the image. Use that whenever possible. Loading a bunch of full-size photos needs a lot of memory, so change to loading smaller images whenever your UI allows smaller sizes.
Beyond that you can change how you handle images using one of the following strategies. In ascending order of complexity (and effectiveness):
Make sure "Allows external storage" is enabled for the image property in your data model. This will let Core Data put the images outside the persistent store without requiring you to manage those files. This will save on memory if, for example, you sometimes fetch data but aren't using the image property.
Change the data model so that the image is saved in a different entity, with a relationship linking it to your current entity. This should make it easier to avoid "accidentally" loading images when you're not using them.
Put the images in separate files and keep only the file names in Core Data. You can still sync the images via iCloud, because you can sync files directly via iCloud outside of Core Data. But you'll need extra code to manage uploading/downloading the images. You'll also need to make sure you can handle the case where Core Data has finished syncing but the image is not available yet.
On this list, #1 is easiest but will probably have the least effect. Using #3 should be very effective but will require the most work.
I want users to save the picked image and video within the app. So I wanted to know what is the best. Should i convert the picked images and videos to NSData and save in Core data? I search everywhere and everyone recommend to use Documents Folder to save images and videos because coredata is slower. I just started learning CoreData so I don't have much knowledge about it. Here is what I have come up.
Users pick the image or video -> App Saves it to documents folder -> Using coredata, application stores the filepath as string to access the image or video. (and User can backup anytime using icloud)
and Users can access those files in their other devices using iCloud. Please explain me if I'm incorrect or if you have better alternative or the question is wrong in any way. Thanks
I've done both ways and for me it's just a matter of how to use the database. If you'd like to to be able to backup the database and share with other, I would recommend storing files in CoreData as NSData. If it's just a local database it might be easier to store URL's to images stored on disk.
When you setup a Binery field in CoreData you can optionally select "Allows External Storage" which basically store the file on disk for you, but you can load it from the databas as if it was stored in the db. This is what I use most for images.
With this option, it is easy to create a zip file of the database and all binary files connected to it and also easy to unzip it to make a restore of the database.