UIImageView iphone应用程序中无法加载图像

时间:2011-07-24 18:47:49

标签: iphone objective-c ios uiimageview uiimage

我正在尝试仅在首次启动应用时在UIImageView中显示图像。因此,在使用NSUserDefaults成功检测后,我尝试在MainViewController中加载名为firstLaunch.png的图像。

我创建了UIImageView IBOutlet并连接,一切正常,但图像没有加载。虽然稍后在我的代码中的其他地方,我可以成功更新UIImageView。这是我尝试将图像加载到UIImageView的行:

[countDownImage setImage:[UIImage imageWithContentsOfFile:@"firstLaunch.png"]];

1 个答案:

答案 0 :(得分:3)

您可以尝试获取文件的路径 因此,假设图像位于应用程序包中,请使用以下路径获取路径:

NSString *imgPath = [NSBundle mainBundle] pathForRessource:@"firstLaunch"
                                                    ofType:@"png"];
[countDownImage setImage:[UIImage imageWithContentsOfFile:imgPath]];

另一种方式是:

[countDownImage setImage:[UIImage imageNamed:@"firstLaunch.png"]];