如何在资源文件夹中加载图像?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码应该可以工作。 (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.