FileExistsAtPath returning NO on movie from UIImagePickerController - iphone

I saved a movie file's path from UIImagePickerController, and I know it exists because I can play it on the device. An NSLog on the string containing the movie file path returns this:
file://localhost/private/var/mobile/Applications/E694555D-3959-4CC5-A829-4260323C2C65/tmp//trim.6JemAI.MOV
When this string is used like this however, it returns NO:
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:media.movie]);
Any idea this this is failing? Could it be related to the value being stored as a path, or perhaps that the path includes // at one point? These are just some thoughts I've had.

You need to convert the URL to a file path.
NSURL *url = info[UIImagePickerControllerMediaURL];
NSString *path = [url path];
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:path]);
A path doesn't have the leading file://localhost.

Related

Check if file is already saved to disk?

I am downloading files from my server, saving them to device, and displaying them to the user in my app. I want to implement a check to see if the file already exists on the device so we can skip the download and just display, but I can't figure out the best way to do that.
I create a unique fileName for each file and then convert it to an NSURL like this:
NSString *fileString = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:fileName]];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
Then I write to file and save the URL for use shortly after:
[data writeToFile:fileString atomically:YES];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];
How can I check if that File or URL already exists?
Thanks
NSFileManager has a method to check if a file exists at a path:
[[NSFileManager defaultManager] fileExistsAtPath: fileString]
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000305-CHDDDDJG

Read Image file from custom directory using NSBundle

Creating a custom directory which has all the images. Designing it custom because, it will help me to get the images as an when I need at various places in the configuration.
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
[filemgr createDirectoryAtPath: #"/Users/home/lifemoveson/test" withIntermediateDirectories:YES attributes: nil error:NULL];
I have placed images under test folder and are of .png types.
Is there a way to retrieve the images like below.
/** UPDATED again **/
Currently this folder is under Application_Home/Resources/ImageTiles/ as per Photoscroller example.
How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ?
- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col
{
// we use "imageWithContentsOfFile:" instead of "imageNamed:" here because we don't want UIImage to cache our tiles
NSString *tileName = [NSString stringWithFormat:#"%#_%d_%d_%d", imageName, (int)(scale * 1000), col, row];
// Currently this folder is under <Application_Home>/Resources/ImageTiles/ as per Photoscroller example.
// How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ?
NSString *path = [[NSBundle mainBundle] pathForResource:tileName ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
}
Applications running on iOS are sandboxed; you can't simply create directories wherever you please. Your createDirectoryAtPath: call will fail. You should use one of the directories set aside for your application instead.
Once you obtain the path for one of those directories, getting the path for files within them is simply a case of using NSString's stringByAppendingPathComponent: method.
Makin a call to the functions such as
NSString* newDirPath = [self createDirectoryWithName:#"Test"];
if (newDirPath) {
[self saveFile:#"MasterDB.sqlite" atPath:newDirPath];
}
which are implemented as follows
-(NSString*)createDirectoryWithName:(NSString*)dirName{
NSArray* directoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES);
NSString* directoryPath = [directoryArray objectAtIndex:0];
NSString* newPath = [directoryPath stringByAppendingString:[NSString stringWithFormat:#"/%#",dirName]];
NSFileManager *filemamager = [NSFileManager defaultManager];
BOOL flag = [filemamager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes: nil error:NULL];
return flag == YES ?newPath: nil;
}
-(BOOL)saveFile:(NSString*)fileName atPath:(NSString*)path{
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the File and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the File has already been created in the users filesystem
NSString *filePath = [path stringByAppendingPathComponent:fileName];
success = [fileManager fileExistsAtPath:filePath];
// If the File already exists then return without doing anything
if(success) return YES;
// If not then proceed to copy the File from the application to the users filesystem
// Get the path to the database in the application package
NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
// Copy the database from the package to the users filesystem
NSError *error = nil;
BOOL flag = [fileManager copyItemAtPath:pathFromApp toPath:filePath error:&error];
return flag;
}
might help you solve your problem. Creating a directory is achieved using first function and the second will let you save files from your application bundle to any of the previously created directory. I hope you can modify it save files located at places other than Application bundle to suit to your need.

How should I specify the path for an sqlite db in an iPhone project?

After adding it as a resource, the database file itself is in the project root.
I've only been able to open it by specifying the full path as OS X sees it, i.e., "/Users/Louis/Documents/Test Project/test.db".
But of course there is no such path on an iPhone.
I think I should define the path as "application root/test.db" but I don't know how, or if that would even work anywhere else besides my development machine.
Thanks for any ideas.
To get the path of the file you've added in XCode you would use pathForResource:ofType: with your mainBundle.
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourDb" ofType:#"sqlite"];
But you can't change files in the mainBundle. So you have to copy it to another location. For example to the library of your app.
You could do it like this:
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *targetPath = [libraryPath stringByAppendingPathComponent:#"yourDB.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:targetPath]) {
// database doesn't exist in your library path... copy it from the bundle
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:#"yourDb" ofType:#"sqlite"];
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:targetPath error:&error]) {
NSLog(#"Error: %#", error);
}
}
Don't just use the SQLite API, use this amazing wrapper called FMDB: https://github.com/ccgus/fmdb
Getting Paths to Standard Application Directories

NSFilemanager one directory back?

i want to know if there is a way to get the previous directory when working with NSFileManager...
This can be probably done by appending '..' to the current directory
([[NSFileManager defaultManager] currentDirectoryPath] ) , but it's not a good idea at all :(
Is there another effective way to do this ?
If what you're trying to do is just obtain the string representing the path of the parent directory then you could do this:
NSString* parentPath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByDeletingLastPathComponent];
If you actually want to change to the parent directory, then just:
[[NSFileManager defaultManager] changeCurrentDirectoryPath:#".."];
Sure, just use the stringByDeletingLastPathComponent method in NSString. As per your question, you'd do something like:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* currentDirectoryPath = [fileManager currentDirectoryPath];
NSString* previousDirectory = [currentDirectoryPath stringByDeletingLastPathComponent];

Get file property

How to get file size of pdf,gif,doc etc using xcode.
suppose to i get pdf file form resorce folder so how can i get size of for this file.
is there any way?
Thanks you,
If you want to calculate the size of your resource file in run-time basically you can do the following (omitting error checks etc):
// Get path for resource file
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"myPDFFile" ofType:#"pdf"];
// Get file attributes
NSDictionary* attributeDict = [[NSFileManager defaultManager] attributesOfItemAtPath:resourcePath error:nil];
// Get size attribute
NSNumber* fileSizeObj = [attributeDict objectForKey:NSFileSize];
long long fileSizeVal = [fileSizeObj lonLongValue];