Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-14-2010, 05:16 PM   #1 (permalink)
New iPhone-iPad Developer
 
Join Date: May 2010
Posts: 24
JuanMazuera is on a distinguished road
Red face 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...

thanks in advance
JuanMazuera is offline   Reply With Quote
Old 06-14-2010, 05:23 PM   #2 (permalink)
vja
Registered Member
 
Join Date: Jan 2009
Location: Izhevsk, Russia
Age: 27
Posts: 42
vja is on a distinguished road
Send a message via Skype™ to vja
Default

Quote:
Originally Posted by JuanMazuera View Post
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...

thanks in advance
hi!

look at NSFileManager methods, for example, result array contains all items in documents directory:

Code:
@implementation Utils

+ (NSString *) documentsDirectory {
	static NSString* dPath = nil;
	
	if (!dPath) {
		dPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
		[dPath retain];
	}
	
	return dPath;
}

// ....
NSArray * docsContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: [Utils documentsDirectory]											error: nil];
__________________
be patient, my english is not ideal
vja is offline   Reply With Quote
Old 06-14-2010, 05:26 PM   #3 (permalink)
New iPhone-iPad Developer
 
Join Date: May 2010
Posts: 24
JuanMazuera is on a distinguished road
Default

Quote:
Originally Posted by vja View Post
hi!

look at NSFileManager methods, for example, result array contains all items in documents directory:

Code:
@implementation Utils

+ (NSString *) documentsDirectory {
	static NSString* dPath = nil;
	
	if (!dPath) {
		dPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
		[dPath retain];
	}
	
	return dPath;
}

// ....
NSArray * docsContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: [Utils documentsDirectory]											error: nil];

i'll give it a try, i have read that document, it is just i haven't managed it to work, thanks for your answer
JuanMazuera is offline   Reply With Quote
Old 06-14-2010, 05:53 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by JuanMazuera View Post
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...

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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-14-2010, 05:59 PM   #5 (permalink)
vja
Registered Member
 
Join Date: Jan 2009
Location: Izhevsk, Russia
Age: 27
Posts: 42
vja is on a distinguished road
Send a message via Skype™ to vja
Default

in this case be safe with memory, don't upload to array a lot of images
__________________
be patient, my english is not ideal
vja is offline   Reply With Quote
Old 06-14-2010, 06:08 PM   #6 (permalink)
New iPhone-iPad Developer
 
Join Date: May 2010
Posts: 24
JuanMazuera is on a distinguished road
Default

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 View Post

Use a method like contentsOfDirectoryAtPath:error to get a list of filenames (and folder names) from that subdirectory.
this is what i'm not able to do! thanks!
JuanMazuera is offline   Reply With Quote
Old 06-14-2010, 06:48 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by JuanMazuera View Post
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];
	}
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-14-2010, 07:02 PM   #8 (permalink)
New iPhone-iPad Developer
 
Join Date: May 2010
Posts: 24
JuanMazuera is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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];
	}
}
Thank you very much for sharing this code, i'll try it and give my feedback, i'm still kind of slow at developing... still learning!
JuanMazuera is offline   Reply With Quote
Reply

Bookmarks

Tags
documents, documents directory, folder, images, nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 338
9 members and 329 guests
Absentia, Domele, fiftysixty, givensur, heshiming, linkmx, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0