Save image into realm [duplicate] - ios

This question already has answers here:
How to put an image in a Realm database?
(2 answers)
Closed 7 years ago.
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

Related

How to access data in a UIPasteboard copied by Files.app?

I'd like to use Files.app to copy a folder (e.g. from Dropbox or iCloud), then switch to my App and read the contents of this folder for further processing. I can't find a way though to get the actual data from UIPasteboard. Calling loadObject on the NSItemProvider gives me an NSData archive which I can unarchive and then get a (private) FPItem, which implements <NSFileProviderItem>, but what now? How can I actually request downloading the actual folder this item points to?
I've used one of my precious Technical Support Incidents (TSI) to have an Apple engineer give a statement.
The outcome is that at this point of time, copy'n'paste of a folder in Files.app to your own app is not supported. If you want to import a folder, use a UIDocumentPickerViewController.
As I understand it, you would like to download a folder from Dropbox. You may want to consider using the Dropbox API with a URLSession.shared.datatask(...) using a REST request. If you don't want to deal with implementation details, you could use a library like SwiftyDropbox. There isn't really any need to use UIPasteboard because your end goal is downloading a folder to your app. If you would like to save said folder to the Files app, you can do so after you download/process the folder.
Here are some useful links:
Swifty Dropbox Overview
Swifty Dropbox Download Example
Swifty Dropbox API Docs
Dropbox API HTTP (RESTful Requests)
In link 2, the snippet of code below gets the directory to save the file in. One way you could get a whole folder is to (1) create a new folder in the Documents directory like this and (2) iterate through every item in the Dropbox folder and add that to the newly created folder.
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destURL = directoryURL.appendingPathComponent("myTestFile")
To implement (1), you would need to get all the files in a folder, which you can do with this /list_folder API endpoint. Then, to implement (2), you would iterate through all the files given, downloading them like in link 2.
Let me know if my answer helped, or if you need any further clarification. Thanks a bunch! :)

What is the id in URL path? [duplicate]

This question already has an answer here:
What is the hexadecimal part in the Documents folder's path?
(1 answer)
Closed 5 years ago.
Recently I realize that in URL path to local file one path element changed: /private/var/mobile/Containers/Data/Application/A6ED77D7-3A57-47BC-BD41-B1F857529D0F I mean A6ED77D7-3A57-47BC-BD41-B1F857529D0F sometimes it stay the same and sometimes it is different than before.
I found this when after I save the whole path in db I wasn't able to read a file again because FileManager said that file doesn't exist.
I'd like to ask what this id is, when it changes?
That is just a path to the documents directory which will be changed everytime you run the application. Following would be helpful for you: FileManager says file doesn't exist when it does

What different between store database in different locations in iOS?

I'm working with SQLite.swift.
In the document, the path to the database is:
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first!
but i want to import and use an existing database, so i've dragged my existing database to my keyboard extension folder, and create connection to it with path is:
let path = NSBundle.mainBundle().pathForResource("db", ofType:"sqlite3")
So, i've noticed that the first way, the database will be store in /Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Data/PluginKitPlugin/0BC647E4-26F3-4A1F-8271-CC73C96FD197/Documents
and the second way, the database will be store in the app.
/Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Bundle/Application/E5D9514C-859A-4D4D-A771-A8CE9CDCD3E7/AppName.app/PlugIns/AppNameExt.appex
What's different between these two locations?
The second way might increase the app size because it contains the database?
And if i want to Archive/Submit my App to the AppStore with existing database, is this the only way?
The main difference is that storing the file in the documents folder means you can write (update) it, which is pretty important for a database file. You cannot write to a file in the app bundle.
The usual pattern for using a database in an app is:
Create a pre-seeded database during development and copy it to the app bundle during building.
When running, check if the database file exists and is up-to-date in the documents folder.
If not, copy it from the app bundle.
Open the database in the documents folder and read/write as desired.

Get filename UIImagePickerController in swift

How do you get the filename of the picture taken from the camera with swift? I need the filename because I want to get the path and send it to the web server.
let path = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") as String
Thanks.
Please allow me to sort your question and offer different alternatives:
When you take pictures with an iOS device by using the UIImagePickerController or any other available controller, the image is usually saved in a UIImage object and not necessary to the bundle documents folder in the app, unless you decide to do it in the code for further display.
Regarding uploading the image to a web server, it depends on the destination server requirements, therefore, you should check what are the methods available for you to upload the image to that server.
I'm familiar with few options as follow:
1) The destination server requires to deliver the image in multipart/form-data, then you should convert your UIImage to NSData by using one to the two available methods UIImageJPEGRepresentation or UIImagePNGRepresentation depending in which format you are required to send. I suggest you check how to build the call to the destination server method (there are several post in StackOverflow about it)
2) Server requires to deliver the image in base64EncodedString mode, then you need to convert your image as in the following example:
let string: String = "data:image/png;base64," + (UIImagePNGRepresentation(image!)?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength))!
3)The destination server requires a path (meaning they don't store the image): you should first save the picture in an accessible path in the web (not inside the iOS device) by probably using one of the 2 methods I just described above and then, upload the image path you get from the place you uploaded the picture to the destination server.
I hope I succeeded to help you with my answer :-)

iOS Physical Memory Management (File Cache)

I'm storing image files in Documents directory for file caching.
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var path = paths.stringByAppendingPathComponent(imagePath!)
data.writeToFile(path, atomically: true)
What will happen when so many file stored? Is there auto swapping? Or can i detect it with code?
NOTE: I don't want to use tmp directory.
That's what i want. Thanks #gnasher729.
Put data cache files in the Library/Caches/ directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed.
There is no Auto Swipe mechanism to handle such task. It's user data and user should have control over it, when to write and when to delete. You can program such task within application. You need to decide if you want
1. To keep a check based upon no of days from file creation or
2. To keep a check on no of maximum files that are allowed within directory.
You can run this task in background every time while launching the application .
You can delete individual files using below code. Check this Question
[[NSFileManager defaultManager] removeItemAtPath: pathToFile error: &error];
Note - I recommend going through iOS Data Storage Guidelines to figure out which directory will suit you better

Resources