Create NSMutableArray with all objects in Subfolder
Hi everybody!
I've been looking for this all over the web but i have not found a good answer i hope someone could help me.
how can I do to start a NSMutableArray with all the objects (images) in a specific subfolder inside the documents folder at the app startup? this folder can contains any number of files...
I've been looking for this all over the web but i have not found a good answer i hope someone could help me.
how can I do to start a NSMutableArray with all the objects (images) in a specific subfolder inside the documents folder at the app startup? this folder can contains any number of files...
thanks in advance
hi!
look at NSFileManager methods, for example, result array contains all items in documents directory:
I've been looking for this all over the web but i have not found a good answer i hope someone could help me.
how can I do to start a NSMutableArray with all the objects (images) in a specific subfolder inside the documents folder at the app startup? this folder can contains any number of files...
thanks in advance
You want a mutable array that contains UIImage objects that are loaded from a folder at startup?
Use NSFileManager defaultFileManager to get a pointer to the system's file manager object.
Create a string that cottons a path to your images folder by first getting the path to the documents folder, and then appending the subfolder name to the end.
Use a method like contentsOfDirectoryAtPath:error to get a list of filenames (and folder names) from that subdirectory.
loop through those names, skipping the folder names.
For each filename, use CGImage imageWithContentsOfFile to load that image.
Then add that image to your mutable array and continue.
You'll have to do a certain amount of research to put that code together, but it isn't rocket science. It should be a method about 1/2 page long.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
thanks for your answer... actually of that 1/2 page long code i'm pretty advanced, i'm able to do almost everything you said, even the memory management as vja said, the only problem is to start he array with those files already existing, I think is the contentsOfDirectoryAtPath:error method that i'm not being able to use well, can you point me somewhere to implement that code correctly?
Quote:
Originally Posted by Duncan C
Use a method like contentsOfDirectoryAtPath:error to get a list of filenames (and folder names) from that subdirectory.
thanks for your answer... actually of that 1/2 page long code i'm pretty advanced, i'm able to do almost everything you said, even the memory management as vja said, the only problem is to start he array with those files already existing, I think is the contentsOfDirectoryAtPath:error method that i'm not being able to use well, can you point me somewhere to implement that code correctly?
this is what i'm not able to do! thanks!
Here is some code I cut out of a Mac OS app I wrote a while back.
I did a quick-and-dirty edit on it to how what you need to do. It won't be perfect:
Code:
int index;
NSError* result;
NSArray* theFilesArray = [fileMgr contentsOfDirectoryAtPath:documentsPath error: result];
//Check for errors here.
unsigned int num_files = [theFilesArray count];
NSMutableArray* theImagesArray = [NSMutableArray arrayWithCapacity:10];
UIImage* anImage;
for (index = 0;index < num_files; index++)
{
theName = [theFilesArray objectAtIndex: index];
//NSLog(@"Path = %@", theName);
thePath = [documentsPath stringByAppendingPathComponent: theName];
fileExists = [fileMgr fileExistsAtPath:thePath isDirectory: &isDirectory];
if (fileExists && !isDirectory && [[thePath pathExtension] isEqualToString: @"png"]) //replace with the file extension you're using
{
anImage = [UIImage* imageWithContentsOfFile: thePath];
if (anImage)
[theImagesArray addObject: anImage];
}
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.