0

I have a folder of png's called "cards" in Supporting Files. I'm trying to set a pic as a UIImage instance variable for a object. When i try to NSLog the UIImage i get null.

I don't think I'm accessing the path to the pics correctly, but not sure ????

#import "Deck.h"
#import "Card.h"

@implementation Deck

@synthesize cards;

- (id) init
{
    if(self = [super init])
    {
        cards = [[NSMutableArray alloc] init];

        NSInteger aCount, picNum = 0;

        for(int suit = 0; suit < 4; suit++)
        {
            for(int face = 1; face < 14; face++, picNum++)
            {

                //NSString *path = [[NSBundle mainBundle] bundlePath];
                //NSString *imagePath = [path stringByAppendingPathComponent: [NSString stringWithFormat:@"/cards/card_%d.png",picNum]];

                NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum];
                NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"inDirectory:@"/cards"];

                NSLog(@"%@", path);
                UIImage *output = [UIImage imageNamed:path];

                //NSLog(@"%@", output);

                Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
                                                countValue:(NSInteger)aCount
                                                suit:(Suit)suit
                                                cardImage:(UIImage *)output];

                [cards addObject:card];
            }

        }
    }
    return self;
}

}

@end

blitzeus
  • 485
  • 2
  • 10
  • 28
  • Why are you casting every single argument of `- [Card init...]`? –  Dec 28 '12 at 16:03
  • its in a loop initializing 52 Card objects and then adds them to an array, i just didnt post all the code – blitzeus Dec 28 '12 at 16:57

1 Answers1

1

The conceptually better approach would be to use the method of NSBundle which was specifically designed for working with subdirectories:

NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName
                                                 ofType:@"png"
                                            inDirectory:@"cards"];
  • this still outputs null when i NSLog the UIImage – blitzeus Dec 28 '12 at 16:57
  • Are the files actually present in the filesystem? Are they valid images? –  Dec 28 '12 at 17:29
  • i can select a file through IB and display it but I want to do it strictly by code – blitzeus Dec 28 '12 at 17:48
  • when i NSLog fileName with H2C0C3's code, it displays all the file names but when I NSLog path it still gives null – blitzeus Dec 28 '12 at 17:50
  • @blitzeus Then the files are not in the `cards` folder. Are you sure there's an actual folder in the bundle and not just a category in Xcode? –  Dec 28 '12 at 18:00
  • @blitzeus See, the project is set up well. I don't see any reason why it would not work. It may be just Xcode that is buggy and it doesn't copy over the files. Try cleaning the project maybe. –  Dec 28 '12 at 19:54