我在支持文件中有一个名为“cards”的png文件夹。我正在尝试将pic设置为对象的UIImage实例变量。当我尝试NSLog UIImage时,我得到null。
我认为我没有正确访问照片的路径,但不确定????
#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
答案 0 :(得分:1)
概念上更好的方法是使用专门用于处理子目录的NSBundle
方法:
NSString *fileName = [NSString stringWithFormat:@"card_%d", picNum];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName
ofType:@"png"
inDirectory:@"cards"];