【问题标题】:access an UIImage instance variable and display it in UIImageView访问 UIImage 实例变量并将其显示在 UIImageView
【发布时间】:2012-12-28 19:40:06
【问题描述】:

我正在尝试访问 UIImage 实例变量并将其显示在 UIImageView 中。当我尝试 NSLog 路径时,我得到空值。我可以通过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;
}

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

Link

【问题讨论】:

    标签: uiimageview uiimage nsbundle ivar


    【解决方案1】:

    你不必包含图片的所有路径,如果你只输入名称,Xcode 会自动查找它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多