2

I have a folder with pictures in my project and i like to know how i could put this pictures from the folder into a array

How should i do that?

I tried this to put the images in the array

    UIImage*image = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
    NSArray*images = [[NSMutableArray alloc]initWithContentsOfFile:image];
T1992
  • 73
  • 8
  • Do you have the images in a "group" or in a directory? If you just dragged them into xCode check if you have them in the correct directory. It's really easy to mess up with groups and folders... Happened to me too... – Git.Coach Dec 10 '12 at 16:13
  • Also something that seems most of the answers replicate while its wrong... you cannot pass multiple strings to the method (I'm referring to the `@"jpg" @"jpeg" @"gif"` part)... – Alladinian Dec 10 '12 at 16:16
  • The problems here are manifold. The setting of `UIImage *image` with `pathsForResourcesOfType`, which returns `NSArray` of `NSString`. The incorrect syntax for `pathsForResourcesOfType` (which most of us glossed over, focusing on other more obvious problems). The notion that you could pass either a `UIImage` or a `NSArray` to `initWithContentsOfFile`, which really needs a `NSString`. And even if the `initWithContentsOfFile` was correct, the notion of using that with a `NSMutableArray` makes no sense. I've taken a crack at answering this with a complete rewrite. – Rob Dec 10 '12 at 16:36

5 Answers5

5

You could do the following, getting the array of filenames and then filling another array with the images, themselves (assuming that's what you were trying to do).

NSMutableArray *imagePaths = [[NSMutableArray alloc] init];
NSMutableArray *images = [[NSMutableArray alloc] init];

NSArray *imageTypes = [NSArray arrayWithObjects:@"jpg", @"jpeg", @"gif", nil];

// load the imagePaths array

for (NSString *imageType in imageTypes)
{
    NSArray *imagesOfParticularType = [[NSBundle mainBundle]pathsForResourcesOfType:imageType 
                                                                        inDirectory:@"Images"];
    if (imagesOfParticularType)
        [imagePaths addObjectsFromArray:imagesOfParticularType];
}

// load the images array

for (NSString *imagePath in imagePaths)
    [images addObject:[UIImage imageWithContentsOfFile:imagePath]];

As an aside, unless these are tiny thumbnail images and you have very few, it generally would not be advisable to load all the images at once like this. Generally, because images can take up a lot of RAM, you would keep the array of filenames, but defer the loading of the images until you really need them.


If you don't successfully retrieve your images, there are two questions:

  1. Are the files included in my bundle? When you select the "Build Phases" for your Target's settings and expand the "Copy Bundle Resources" (see the image below), do you see your images included? If you don't see your images in this list, they won't be included in the app when you build it. To add your images, click on the "+" and add them to this list.

    enter image description here

  2. Are the files in a "group" or in an actual subdirectory? When you add files to a project, you'll see the following dialog:

    enter image description here

    If you chose "Create folder references for added folders", then the folder will appear in blue in your project (see the blue icon next to my "db_images" folder in the preceding screen snapshot). If you chose "create groups for added folders", though, there will be the typical yellow icon next to your "Images" group. Bottom line, in this scenario, where you're looking for images in the subdirectory "Images", you want to use the "Create folder references for added folders" option with the resulting blue icon next to the images.

Bottom line, you need to ensure the images are in your app bundle and that they are where you think they are. Also note that iOS is case sensitive (though the simulator is not), so make sure you got the capitalization of "Images" right.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • When I compile the code, he says that the array is empty but i do have a folder within my project with images – T1992 Dec 11 '12 at 09:27
  • @T1992 I just tested the code and it works fine. The question is whether the images are in the "`Images`" folder like you think they are. See my updated answer above. – Rob Dec 11 '12 at 13:55
  • you were right, he couldn't find the pictures in the directory thanks :) – T1992 Dec 12 '12 at 08:44
0

If I am understanding your question correctly initWithContentsOfFile doesn't do what you are expecting, per the documentation:

"Initializes a newly allocated array with the contents of the file specified by a given path."

Additionally, pathsForResourceOfType is already creating an array, not a UIImage, you can simply do:

NSArray* images = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];
0
[[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];

already returns an array of these objects. Change your line to this:

 NSArray *images = [[NSBundle mainBundle]pathsForResourcesOfType:@"jpg" @"jpeg" @"gif" inDirectory:@"Images"];

Note that this array will only hold the paths for all your images. In order to make images of them you need to call

for(NSString* imagePath in images) {

    UIImage* anImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
    //do something with your image here.
}

Hope that helps

Boris Prohaska
  • 912
  • 6
  • 16
0

Read the documentation of initWithContentsOfFile method of NSArray:

The array representation in the file identified by aPath must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.

In your case you need to use NSFileManager to enumerate files in directory. Here is the example of directory enumeration from documentation:

NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
    [localFileManager enumeratorAtPath:docsDir];

NSString *file;
while (file = [dirEnum nextObject]) {
    if ([[file pathExtension] isEqualToString: @"doc"]) {
        // Create an image object here and add it to 
        // mutable array
    }
}
[localFileManager release];
Mikhail Grebionkin
  • 3,806
  • 2
  • 17
  • 18
0

Try this:

    NSMutableArray* paths=[NSMutableArray new];
    NSFileManager* manager=[NSFileManager new];
    NSBundle* bundle= [NSBundle mainBundle];
    NSDirectoryEnumerator* enumerator= [manager enumeratorAtPath: [bundle bundlePath] ];
    for(NSString* path in enumerator)
    {
        if([path hasSuffix: @".jpg"] || [path hasSuffix: @".jpeg"] || [path hasSuffix: @".gif"])
        {
            [paths addObject: path];
        }
    }

For explanations I suggest that you look at NSDirectoryEnumerator documentation.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187