I want to save photos from photo gallery in my app and after show them in collection View. Is there some way to store this image in array in UserDefaults?
In NSUserDefaults you just must save Dictionary Objects, you must not save big amount of data.
To save big amount of data like images you should take a look to the Sandbox.
Take a look to the tutorial How to save in Sandbox
I would use Core Data and/or store them locally.
see the responses in:
Saving Picked Image to CoreData
NSUserDefaults should only be used for the applications default values.
Related
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.
I was trying to Store data offline by using firebase database reference. I read documentation and it say one line code will store everything.
Database.database().isPersistenceEnabled = true
But this line only store text. It's not storing image offline.
isPersistenceEnabled is only storing your text content. if you want to save image you need to use core data. Core data is little bit hard to save large file. i would recommend to use NSfileManager to Store your image.
Hope it will help .
Do you want to send data after you will be connected to WiFi? So you can store your data to CoreData and after you will be connected to Wifi send it to Firebase storage
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.
So i have this situation with images. In one of app stages i get all user photos from his photo library. I get them as ALAsset's. I let him choose photo he wants. Then i save his chosen photo to applications directory as full size photo with HIGH_ prefix and a thumbnail of a photo with LOW_ prefix. I need this because photos have some properties like time etc. I save those properties to SQL database with a field of photo name that begins with HIGH_ or LOW_. When i need to get photos i get properties from db and then do [UIImage imageWithContentsOfFile:photoPath]. Can someone tell me how to do it more efficient because writing and getting photos like this takes some time. And on iPhone 4 i sometimes even get memory warnings. AND another question would be, how should i save photos fetched from web?
I stand corrected, instead of using core data, Apple writes,
It is better, however, if you are able to store BLOBs as resources on the filesystem, and to maintain links (such as URLs or paths) to those resources. You can then load a BLOB as and when necessary.
So you are doing it correctly, but maybe you should check out transformables. Just make sure you remove images you aren't using from memory if you are getting warnings
From documentation: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html
Under the section 'Large Data Objects (BLOBs)"
Another way to do it with a transformable:
Which way to store data(image)? NSData, String or Transformable
In fact, perhaps transformables are for core data at least:
How should I store UIImages within my Core Data database?
Now I save the cache in NSUserDefaults. As before, I used the small array, the NSUserDefaults for me. Now when you save large array in NSUserDefaults application freeze. What are the alternatives to the cache arrays in objective - c for iOS?
UPDATE:
I want to save 30-60 HTML file into an array. and then the array cache. Stores an array to NSUserDefaults long. that is better for me to use? the database? nscache? if a database, what?
You can write the HTML data as NSString into a NSDictionary and save it in a .plist file in the documents directory. You would be able to edit, append and delete your data as you want using this approach.
If you are looking for a caching pupose,you can use NSCache and iOS manages the memory.But its not guaranteed the availability of data always.iOS may remove data and free up the memory because of memory warning.
If your data is higher in size,you should save the data to document directory instead of directly saving to RAM (Saving to Objects use RAM memory).Write data to a file or use plist.