0

I'm looking for png-files in the folder Documents of my app and add their titles to the array. After calling the function array is still empty. Can you help me? I'm beginner in Obj-C. Thanks.

My function:

-(void)getphotolist{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSBundle *myBundle = [NSBundle bundleWithPath:documentsDirectory];

    NSArray *mypngs = [myBundle pathsForResourcesOfType:@".png"
                                               inDirectory:documentsDirectory];

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

    NSLog(documentsDirectory);

    for (NSString *tString in mypngs) {
        [photo_array addObject:tString];

        for (id obj in photo_array)
        {
            NSLog(@"obj: %@", obj);
        }
    }   
}
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
STBY
  • 209
  • 1
  • 2
  • 10

3 Answers3

1

Try this,

NSString * myBundle = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *dir = [manager enumeratorAtPath: myBundle];

NSString *filename;
NSMutableArray *photo_array = [[NSMutableArray alloc] init];
while ((filename = [dir nextObject] )) {


    if ([filename hasSuffix:@".png"]) {   


        [photo_array addObject:[filename stringByDeletingPathExtension]];


    }

}
Kalpesh
  • 5,336
  • 26
  • 41
0

Problem is here:

NSArray *mypngs = [myBundle pathsForResourcesOfType:@".png"
                                        inDirectory:nil];

You should have "nil" here. It indicates a directory under bundle directory if you have any custom ones.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
-1

Refer this code.

NSString *extension = @"JPEG";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

NSMutableArray *jpegFiles = [NSMutableArray alloc]init];
NSMutableArray *jpegFilesPath=[NSMutableArray alloc]init];
NSString *filename;
for (filename in contents)
{
    if ([[filename pathExtension] isEqualToString:extension])
    {
        [jpegFilesPath addObject:[NSString stringWithFormat:@"%@",[documentsDirectory stringByAppendingPathComponent:filename]]];
        NSLog(@"%@",jpegFilesPath);
        [jpegFiles addObject:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filename]]];

    }
}

Hope this helps you..!!

Mahesh
  • 526
  • 1
  • 6
  • 21
  • Why to iterate through all files and allocate all arrays for that and all string comparations, when you have ready to use file system functions for that ?! – Grzegorz Krukowski Nov 25 '13 at 10:25
  • you have mentioned that you are searching for multiple png files in document folder. So there's obviously need for iteration to get path for all the png files. – Mahesh Nov 25 '13 at 10:30
  • Btw. I'm not the one who is asking the question - anyways why not using pathsForResourcesOfType as it is in question ? – Grzegorz Krukowski Nov 25 '13 at 10:33
  • ohh! sry @GrzegorzKrukowski you haven't asked the question. Anyway i haven't yet used NSBundle for accessing files from document directory, i found NSFilemanager suitable so i used it. But definitely i'll give a try for solution you have provided if its working. – Mahesh Nov 25 '13 at 10:37
  • And way doesn't matter if either of the solution works perfectly. :) – Mahesh Nov 25 '13 at 11:06
  • Ah no ! Not if you promote heavy, non optimal solution ! :) – Grzegorz Krukowski Nov 25 '13 at 13:54