【问题标题】:Unable to load the UIImageView无法加载 UIImageView
【发布时间】:2016-01-25 22:33:37
【问题描述】:

我一直按照“通过开发 iPhone 游戏学习 Objective-C”一书中对 Alien Invaders 的说明进行操作。

我在.m文件中输入了以下代码,然后书上告诉我运行以下代码:

@interface GameViewController ()
    @property (strong) UIImage *playerImage;
    @property (strong) UIImageView *playerView;
@end


@implementation GameViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        //Assign an image to a variable
        self.playerImage = [UIImage imageNamed:@"ship.png"];

        //Create an image view and start it up with out image
        self.playerView = [[UIImageView alloc] initWithImage: self.playerImage];

         //Set the image views bounding rectangle to scale the image.
         self.playerView.frame = CGRectMake(50,400, 32,32);

         //Show the image on the screen.
         [self.view addSubview: self.playerView];
     }
}

我尝试在模拟器中运行代码,但出现以下错误。为什么会这样?

Alien-Invaders[565:10012] Could not load the "backdrop.png" image referenced from a nib in the bundle with identifier "Chris.Alien-Invaders"

【问题讨论】:

    标签: ios objective-c iphone uiimageview nib


    【解决方案1】:

    听起来,如果您查看界面文件 - 有一个引用“backdrop.png”的元素。您需要添加图像或删除引用它的元素。

    【讨论】:

    • 我没有在任何地方引用过“backdrop.png”。我创建了一个 GameViewController 类。我编写的唯一编码是在 .m 文件中(粘贴在我的问题中),这是本书告诉我编码的地方。我只有几页,但书上说我现在应该在运行代码时看到背景。我对 Objective-C 还很陌生,但我觉得这本书漏掉了一些关键点。
    • @C-Smith 这是您开始的新项目,还是您正在添加 GameViewController 的现有项目。听起来像是一个现有项目,可能有一些东西不是你创建的,但它引用了一些不存在的东西。
    • 如果您不想浏览所有内容,只需创建一个新图像并将其命名为“backdrop.png”并将其添加到项目中即可。
    • 我从头开始这个项目,然后在 GameViewController 类中添加。从字面上看,我花了大约 3 页来创建 Alien Invaders。在情节提要中,我从对象库中添加了一个图像视图,并将该图像设置为“backdrop.png”。然后我在 GameViewController.m 文件中编码(如我粘贴的代码所示)。当这本书没有要求我参考“backdrop.png”时,我有点担心......也许这就是错误发生的原因?正如我所说,我对此很陌生。感谢您的帮助,非常感谢。 :)
    • 是的,您的担忧是正确的——这就是问题所在。您基本上已经告诉 imageview 它将显示“backdrop.png”图像,但没有这样的图像。只需将 imageview 的“Image”设置留空/不要添加 imageview,直到您将“backdrop.png”图像添加到您的项目中 - 祝您在 iOS 冒险中好运:)
    【解决方案2】:

    请检查截图,当你将背景.png拖到Xcode项目时你会看到这些屏幕,请点击复制项目如果需要完成按钮将图像复制到你的Xcode项目

    【讨论】:

    • 那么我需要采取什么行动才能使 UIImageView ("backdrop.png") 出现?正如我所提到的,只有我编写的代码在 GameViewController.m 类中。在情节提要上,我将 UIImageView 拖到屏幕上,然后选择了“backdrop.png”图像。
    • 您可以检查 Build Phases -> Copy Bundle Resources 并检查您的图像是否存在。
    • 我点击了它,但出现一条消息说“你需要获得总共 15 声望才能显示”。 :)
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多