如何在资源文件夹中加载图像?

发布于 2024-09-13 21:56:35 字数 1153 浏览 12 评论 0原文

我的 iPhone 项目中有一张图像。它是 PNG,我可以在 Interface Builder 中设置背景,但我想以编程方式执行此操作。

NSLog(@"Before %@", myEasterEgg);
uiImageBackground.backgroundColor = [UIColor purpleColor];
myEasterEgg = [myEasterEgg stringByAppendingString:@"5"];
NSLog(@"After %@", myEasterEgg);
if (CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0) {
    myEasterEgg = @"";
    uiImageBackground.backgroundColor = [UIColor clearColor];
    [uiImageBackground setImage:[UIImage imageNamed:@"dock.png"]];
    uiImageBackground.image = image;
}
[myEasterEgg retain];
NSLog(@"After %@", myEasterEgg);

上面是我按钮中的代码。它不显示dock.png 文件。

更新。我修改了代码,以便 IF 语句始终执行,但它仍然不加载图像。修改后的代码如下!

NSLog(@"Before %@", myEasterEgg);
uiImageBackground.backgroundColor = [UIColor purpleColor];
myEasterEgg = [myEasterEgg stringByAppendingString:@"5"];
NSLog(@"After %@", myEasterEgg);
    myEasterEgg = @"";
    uiImageBackground.backgroundColor = [UIColor clearColor];
    [uiImageBackground setImage:[UIImage imageNamed:@"dock.png"]];
    uiImageBackground.image = image;
[myEasterEgg retain];
NSLog(@"After %@", myEasterEgg);

I have an image in my iPhone Project. It's a PNG and I can set the background in Interface Builder but I want to do this programatically.

NSLog(@"Before %@", myEasterEgg);
uiImageBackground.backgroundColor = [UIColor purpleColor];
myEasterEgg = [myEasterEgg stringByAppendingString:@"5"];
NSLog(@"After %@", myEasterEgg);
if (CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0) {
    myEasterEgg = @"";
    uiImageBackground.backgroundColor = [UIColor clearColor];
    [uiImageBackground setImage:[UIImage imageNamed:@"dock.png"]];
    uiImageBackground.image = image;
}
[myEasterEgg retain];
NSLog(@"After %@", myEasterEgg);

The above is the code in my button. It doesn't display the dock.png file.

UPDATE. I'VE MODIFIED THE CODE SO THE IF STATEMENT WILL ALWAYS EXECUTE AND IT STILL DOESN'T LOAD THE IMAGE. MODIFIED CODE BELOW!

NSLog(@"Before %@", myEasterEgg);
uiImageBackground.backgroundColor = [UIColor purpleColor];
myEasterEgg = [myEasterEgg stringByAppendingString:@"5"];
NSLog(@"After %@", myEasterEgg);
    myEasterEgg = @"";
    uiImageBackground.backgroundColor = [UIColor clearColor];
    [uiImageBackground setImage:[UIImage imageNamed:@"dock.png"]];
    uiImageBackground.image = image;
[myEasterEgg retain];
NSLog(@"After %@", myEasterEgg);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甚是思念 2024-09-20 21:56:35

该代码应该可以工作。 (1) 测试永远不会通过,因此永远不会调用加载图像,或者 (2) dock.png 文件尚未正确添加到项目中,因此它会加载到应用程序包中。

The code should work. Either (1) the test never passes so the load image is never called or (2) the dock.png file has not been properly added to the project so that it is loaded in the app bundle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文