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 12-10-2011, 07:12 PM   #1 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Question Create array out of all images in app?

Hey,

Is it possible to create a NSArray out of all .png, .jpg, and .jpeg that are all in my app? I know it is possible to do it to a certain type like only .pngs but I am not sure how to do it to all images in my app.

So this is the code I am using and I put it in my ViewDidLoad because that is where I want to fill up my array but the issue is that, according to my NSLogs it is only reaching NSLog "Level1" and it never reaches level 3 thus, my array is nil and causes my app to crash. I have a 'images' group in my Xcode project not a folder if that makes any difference.

Does anyone have any ideas why this is not working?

Thanks!
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8

Last edited by Objective Zero; 12-11-2011 at 03:28 PM.
Objective Zero is offline   Reply With Quote
Old 12-10-2011, 08:06 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
Hey,

Is it possible to create a NSArray out of all .png, .jpg, and .jpeg that are all in my app? I know it is possible to do it to a certain type like only .pngs but I am not sure how to do it to all images in my app.

So this is the code I am using and I put it in my ViewDidLoad because that is where I want to fill up my array but the issue is that, according to my NSLogs it is only reaching NSLog "Level1" and it never reaches level 3 thus, my array is nil and causes my app to crash. I have a 'images' group in my Xcode project not a folder if that makes any difference.

Code:
// Do any additional setup after loading the view, typically from a nib.
NSString * pathToImages = [NSString stringWithFormat: @"%@/images", [[NSBundle mainBundle] 

]];
NSFileManager *localFileManager= [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:pathToImages];
NSString *file;
NSLog(@"level1");
while (file = [dirEnum nextObject]) {
    NSLog(@"level2");
    if(file)
    {
        NSLog(@"level3");
        [imagesArray addObject:file];
    }
}
Does anyone have any ideas why this is not working?

Thanks!
There's lots of problems with your code.

1. Don't create an instance of NSFileManager. Use the class method defaultManager to get a pointer to the shared file manager object.

2. Don't use the file manager at all. Use one of the NSBundle method for listing the contents of a bundle, like pathsForResourcesOfType:inDirectory, or URLsForResourcesWithExtension:subdirectory:

If you have several different types of image files, you will need to call the method repeatedly for each filetype.

You could also get the path to the main bundle's resources directory, use the NSString method "stringByAppendingPathComponent" to add your "/images/" subdirectory to the path, and use the file manager method contentsOfDirectoryAtPath:error to get a list of all the files in the directory. You'd then need to use the NSString method lastPathComponent on each path in the array to get rid of all the path info and get just the filename for each file.
__________________
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 online now   Reply With Quote
Old 12-10-2011, 08:23 PM   #3 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

The moment you posted this I found my answer, I added the folder to my project wrong so I did so re-naming/re-filing and it ended up working! But I will also take your tips into consideration!
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Reply

Bookmarks

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: 405
11 members and 394 guests
7twenty7, AppsBlogger, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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