I am having some problems with NSMutableArray. I have a folder of pictures that I am trying to load into an array so I can access them in my application. I have the following code:
Example.h
Example.m
My folder contains 600 pictures and I would expect that the array size would be 600, but with this code nothing even happens. If I uncomment the tempArray before the for loop and add the picture to that array, it works fine for the loadPicturesIntoArray method, but I can't access the array in the viewPictures method. I tried having the temp array and than setting the picturesArray to the temp array, but it doesn't seem to be copying the pictures over.
I feel like I'm missing something really easy, but I can't seem to figure out what?
Any advice?
Example.h
Code:
#import <Cocoa/Cocoa.h>
@interface Example : NSObject <NSApplicationDelegate> {
NSWindow *window;
// data
NSMutableArray *picturesArray;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) NSMutableArray *picturesArray;
- (IBAction)loadPicturesIntoArray:(id)sender;
- (IBAction)viewPictures:(id)sender;
@end
Example.m
Code:
@implementation Example
@synthesize window;
@synthesize picturesArray;
- (IBAction)loadPicturesIntoArray:(id)sender {
// delete anything that may be in the picturesArray
[picturesArray removeAllObjects];
// find out how many pictures are in the pics folder
NSFileManager *fm = [[NSFileManager alloc] init];
NSArray *contents = [NSArray arrayWithArray:[fm contentsOfDirectoryAtPath:@"/Users/username/pics" error:nil]];
NSInteger numberOfPictures = [contents count] - 1; // because contents grabs the pics and .DS_Store
[fm release];
// load the pictures into the picturesArray
NSString *path = @"";
//NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:numberOfPictures];
for (int i = 0; i < numberOfPictures; i++) {
// get the path to the picture
path = [NSString stringWithFormat:@"/Users/username/pics/image%d.jpeg", (i+1)];
// get the picture
NSImage *picture = [[NSImage alloc] initWithContentsOfFile:path];
// load the picture into the picturesArray
[picturesArray addObject:picture];
//NSRunAlertPanel(@"Image Details:", @"Path: %@\nArray Size: %ld", NULL, NULL, NULL, path, [picturesArray count]);
[picture release];
}
}
- (IBAction)viewPictures:(id)sender {
NSLog(@"Array Size: %ld", [picturesArray count]);
}
My folder contains 600 pictures and I would expect that the array size would be 600, but with this code nothing even happens. If I uncomment the tempArray before the for loop and add the picture to that array, it works fine for the loadPicturesIntoArray method, but I can't access the array in the viewPictures method. I tried having the temp array and than setting the picturesArray to the temp array, but it doesn't seem to be copying the pictures over.
I feel like I'm missing something really easy, but I can't seem to figure out what?
Any advice?