Question

My knowledge about objective c is limited, I have found a way to do what I need. I just don't think it's efficient at all.

I'm trying to add all images from a directory outside the application bundle to the MWPhotoBrowser. Problem is, they wont have specific names for ex. 2f5h3h5y.png.

NSMutableArray *photos = [[NSMutableArray alloc] init];

NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSString *filePath = @"/Library/Application Support/Testing/Testing";
NSString *absolutePath;



MWPhoto *photo;
switch (indexPath.row) {
    case 0:

        myArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:NULL] mutableCopy];

        NSLog(@"~~~~~~~~~~~~MY ARRAY COUNT: %d", [myArray count]);

        if ([myArray count] == 1){

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:0]];

            photo = [MWPhoto photoWithFilePath:absolutePath];
            [photos addObject:photo];

        } else if ([myArray count] == 2){

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:0]];

            photo = [MWPhoto photoWithFilePath:absolutePath]; 
            [photos addObject:photo];

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:1]];

            photo = [MWPhoto photoWithFilePath:absolutePath];
            [photos addObject:photo];

        } else if ([myArray count] == 3){

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:0]];

            photo = [MWPhoto photoWithFilePath:absolutePath];
            [photos addObject:photo];

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:1]];

            photo = [MWPhoto photoWithFilePath:absolutePath];
            [photos addObject:photo];

            absolutePath = [NSString stringWithFormat:@"%@/%@", filePath, [myArray objectAtIndex:1]];

            photo = [MWPhoto photoWithFilePath:absolutePath];
            [photos addObject:photo];

        }

I know this method will not work because I can't set up a 1000 of "if statements." A crash would result if they were out of bounds. Can anyone provide a working example for what I need. An explanation would be appreciated. Thanks

Was it helpful?

Solution

You need to learn about loops.

NSMutableArray *photos = [[NSMutableArray alloc] init];
NSString *filePath = @"/Library/Application Support/Testing/Testing";
NSArray *myArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil];
for (NSString *filename in myArray) {
    NSString *fullPath = [filePath stringByAppendingPathComponent:filename];
    MWPhoto *photo = [MWPhoto photoWithFilePath:fullPath];
    [photos addObject:photo];
}

BTW - this code will only work in the simulator since you are hard coding the library path.

This code also assumes that the only files in the directory are image files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top