0

我在支持文件中有一个名为“卡片”的 png 文件夹。我正在尝试将 pic 设置为对象的 UIImage 实例变量。当我尝试 NSLog UIImage 我得到空值。

我认为我没有正确访问图片的路径,但不确定????

#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;
}

}

@结尾

4

1 回答 1

1

概念上更好的方法是使用NSBundle专门为处理子目录而设计的方法:

NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName
                                                 ofType:@"png"
                                            inDirectory:@"cards"];
于 2012-12-28T16:07:43.753 回答