xCode: Retrieving images from apps documents directory - ios

I've come across a problem I can't find a solution to. I've searched the forums extensively but can't find anything. Hopefully someone might be able to shed some light.
I have an app that can save images. They are saved to the apps documents directory and the path to them is saved in a sqlite database. Every time I run/build the app, it can no longer retrieve the images that are already in the documents directory. During that same build phase I can re-save the images and then I can retrieve them just fine, but as soon as I run/build the app again it can't retrieve the images. The images are not being deleted and the path is still correct.
This is how I try retrieving them in my ViewDidLoad method
//database was opened. Getting path to image
NSString *logoPath = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 21)];
//use path to load image to a UIImage view
[self.logoView setImage:[UIImage imageWithContentsOfFile:logoPath]];
I have confirmed that the image is in the documents directory still and the path is correctly assigned in my first line of code above. This code only fails when I run/build the app. Once the app is running I can save images to the directory and retrieve them with this same code just fine, but once I run/build the app again, the images I saved on the previous build can't be pulled up even though I can see them in the directory still (I use iExplorer to view my iPhones directories).
Any ideas? Im thinking that maybe I have to initiate those images in the directory somehow in order to use them right after I build.
UPDATE - Some more info in case it will help. After the app is built on my iPhone, I can close the app and reopen it and the images work just fine. I can even completely close the app (double click home button and end app process) and when I relaunch the app the images are retrieved from the directory just fine. But as soon as I build the app again through xCode, I cannot access the images. Something is changing when a new build is done.
UPDATE 2 - I think I found the issue and it is in the path.
This is my path
/var/mobile/Containers/Data/Application/8A387845-7B1F-4228-9E7E-2498EC302C5A/Documents/RYal7UXrLvcompanyLogo.png
After I build the app again, this is now the new path when I save the image again
/var/mobile/Containers/Data/Application/C0C30E2F-05D8-48F4-A0FF-603359AF5130/Documents/RYal7UXrLvcompanyLogo.png
Looks like the part in between Applications and Documents changes after every build so the path I am saving in my sqlite db is no longer valid after a new build. So now I need to figure out what that middle part is that keeps changing and how to get it after each build so I can update my paths to my images.

The issue was the path. Every time a new build was created, part of the path to the documents directory changed. See UPDATE 2 in the original question above.

Related

Flutter cannot retrieve iPhone file (sometimes)

I have an app that lets the user pick local files and send them to the backend to add to their vault. To pick the file, I use FilePicker.platform.pickFiles. It seems to work fine, the file is picked and when it's an image we show a preview to the user (and it always works).
Once the user has picked all their files, we create a list of Future to try and send the files to the backend, then we await the list to resolve. If we deem a picture is too big, we reduce the size using FlutterImageCompress.compressAndGetFile.
This usually works, but on iPhone, there is a seemingly random behavior where the file isn't found and an error is thrown : Cannot retrieve length of file, path = '/private/var/mobile/Containers/Data/Application/3600F812-FD42-4441-A654-766991E45E04/tmp/com.feykro.myApp.int-Inbox/myFile.pdf' (OS Error: No such file or directory, errno = 2)
The file in the example works just fine in other instances, the problem seems random and not directly tied to the file I chose. The problem is not exclusive to pdf files either, as it also occurs on images even though they're picked correctly and displayed in the app.
Any idea what could be causing this ?
Thanks in advance !

Flutter-iOS when apps update / recompile stored image is missing

Hi forgive my English and also a newbie on flutter iOS, I have an app on flutter on iOS that users can take pictures and videos from their camera and gallery I'm using the image_picker package this has no problem, then I save it on the on its Application Documents Directory I tried following this guide
my code for getting the application directory is this
Directory _appDocDir;
_appDocDir = await getApplicationDocumentsDirectory();
final Directory _appMediaDir = Directory('${_appDocDir.path}/Pictures/');
return _appMediaDir;
added with a 'Pictures' directory this will give a path of
/Users/macman/Library/Developer/CoreSimulator/Devices/96060868-7E5C-4E15-9900-4D1D4B0B5ACE/data/Containers/Data/Application/BE7728A6-FF15-498E-91A0-A96A3DDE5B31/Documents/Pictures/D_IMG_0002.JPG
from the iOS-simulator I save it and put in the database, and after re-compiling the app or doing a build update the previewing the image/video path is not found anymore. I tried to save another image on this build it and gave this path
/Users/macman/Library/Developer/CoreSimulator/Devices/96060868-7E5C-4E15-9900-4D1D4B0B5ACE/data/Containers/Data/Application/9D634D91-07B8-43BE-8646-2A55833322DF/Documents/Pictures/D_IMG_0005.JPG
I noticed earlier path this part "/BE7728A6-FF15-498E-91A0-A96A3DDE5B31/" is changed every time I re-compile the app or make an update from the iOS device the image is not found. What is the most practice for this users able to save image am I doing it wrong?
Update: I tried changing my logic to change the saved path to only the base name (D_IMAGE_0005.JPG) then when previewing the image will call the current application documents directory but this does not work also.
Thank You
Hi don't save the whole path given by the getApplicationDocumentsDirectory() on you record, instead save only the basename, and every time you preview/load the image call the
getApplicationDocumentsDirectory() + 'Pictures/<basename>',
every time the app gets updated or recompiled on the iOS the appid changes, that is why your path fails to load on a recompile.
iOS removes the documents directory every time you reinstall the app if your application is running on a sandbox environment.
Please refer to this stackoverflow question for more clarification

UIDocument + NSFileWrapper + iCloud = Strange Behaviour

I’m trying to write a fairly simple UIDocument based app and I want the data for each document to be a parent directory containing some image files.
To get started I chose to use Apple’s ShapeEdit sample as that seems to handle lots of the complexity of having files in iCloud. It does only use a single plist file for it’s document data though.
As a first step I got the ShapeEdit app up and running on my iPad and everything works as expected. I have the app and iCloud Drive app running in split view on my iPad so I can see the files ShapeEdit is creating. As expected each time I add a new document a new file appears in iCloud Drive.
To migrate the ShapeEdit app to use more files I moved over to using NSFileWrapper. When ShapeEdit creates a new document it copies a template file. I changed this to just create a new folder using
try fileManager.createDirectoryAtURL(writeIntent.URL, withIntermediateDirectories: false, attributes: nil)
I then modified the UIDocument code to have it read from a NSFileWrapper like this
override func loadFromContents(contents: AnyObject, ofType typeName: String?) throws {
guard let data = contents as? NSFileWrapper else {
fatalError("Cannot handle contents of type \(contents.dynamicType).")
}
fileWrapper = data
I also hobbled some of the other functions in the document to return a simple image for thumbnails, just to get it running and set LSTypeIsPackage to true. Finally I left the shape selection UI there to trigger a document update with
updateChangeCount(.Done)
Again this all works fine. Creating a new document creates a new Directory named Untitled.shapeFile and it shows up in iCloud. Now for odd thing number one. If I open that document and tap on the shape selection UI to trigger a change and wait a few seconds another Untitled.shapeFile directory shows in iCloud. In fact every time I do this I get another one. These seem to only be local to the iPad though as iCloud on my mac only has one copy. Deleting any of these directories within the iCloud Drive app causes all of them to be deleted.
For the next step I added little bit of code to the UIDocument loadFromContents() function
fileWrapper?.addRegularFileWithContents(UIImagePNGRepresentation(image!)!, preferredFilename: "MYImage.png")
so every time the document is loaded it adds another image file to the parent directory.
To begin with this does exactly as I hoped. Open the doc, tap the shape select UI see the MyImage.png file turn up in iCloud Drive inside Untitled.shapeFile. Keep closing and opening and I get more MyImage.png files with some junk on the front which, I assume, is iCloud’s way of preventing files with the same name so all is well there. Except for strange thing number two. Shortly after the MYImage.png file shows in iCloud Drive another one pops up called MyImage 2.png. If I keep closing and opening all the versions of MyImage also get a copy with a 2. My edited version of ShapeEdit never adds any image files with this name format. These extra images do show up on the mac as well so they aren't the same strange phantom copies as the main document directory.
So two things
1) Why do I get multiple copies of the Untitled.shapeFile doc which seem to actually be the same thing?
2) Why do I get extra copies of my image file with a 2 added that my app never had anything to do with?
Any suggestions welcome before this drives me crackers.

resolveLocalFileSystemURL on iOS using different App UUID each time

I am using Cordova fileSystem to download some mp3 Audio files thru my App. I am using cordova.file.cacheDirectory and creating a subfolder within that to store the downloaded files. However, the problem is that each time I make some changes and run the App on iPad I get a different path.
The usual pattern is
file:///var/mobile/Containers/Data/Application/13E9EA83-94A2-4950-811E-E491AC176‌​A78/Library/Caches/MyFolder/MyFiles.mp3
However the long number (App UUID) is not consistent. It changes each time I run the App on iPad through Xcode. Because of this I am unable to access, play, delete any files downloaded during the previous session because I am saving the file names with absolute path in a separate file for quick access in the future.
After some R&D I have realized that even though UUID changes each time, it actually refers to the same location. Hence the solution is not to save absolute path and instead, save only the Path beyond "/MyFolder/" in the above example. Each time when the App is run, the Base Path, which is actually file.cordova.cacheDirectory, should be appended to each entry to obtain the absolute Path to the file.

Downloaded files disappearing on update

I have an app that downloads several pdfs and images. It all work perfectly fine, I store information of those files in core data. My problem comes when I send out updates of the app, all the information in core data is transferred to the new version correctly but the files are nowhere to be found.
I guess I can write a routine to run when users update the app and download the files all over again. I just believe there is a better way to go by preserving the files.
Has anybody experience on this?
You could store the files in the Documents dir, there they will be not deleted unless you remove the app from the device.

Resources