访问一个UIImage实例变量并在UIImageView中显示它



我试图访问一个UIImage实例变量并在UIImageView中显示它。当我尝试NSLog路径时,得到null。我可以通过IB手动显示图片,但我想严格通过代码

来执行此操作
#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 *fileName = [NSString stringWithFormat:@"card_%d", picNum];
                NSString *path = [[NSBundle mainBundle] pathForResource:fileName         
                   ofType:@"png"inDirectory:@"/cards"];
                NSLog(@"%@", path);                          //outputs correctly
                UIImage *output = [UIImage imageNamed:path];
                NSLog(@"%@", output);                        //outputs null
                Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
                                                countValue:(NSInteger)aCount
                                                suit:(Suit)suit
                                                cardImage:(UIImage *)output];
                [cards addObject:card];
            }
        }
    }
    return self;
}

我已经添加了一个链接来显示图片的位置

链接

你不需要包括所有的路径到图像,如果你只输入名称,Xcode会自动查找它

最新更新