gpt4 book ai didi

ios - 从 nsbundle 访问图像

转载 作者:行者123 更新时间:2023-12-01 17:26:19 30 4
gpt4 key购买 nike

我有一个框架试图从包中访问 xib 和图像。我可以正常访问 xib 但无法在 UIImageView 中加载图像。我什至检查文件是否存在并且控制台输出是...

知道如何加载图像吗?

在框架 View Controller 文件中加载 xib 的代码

self = [super initWithNibName:@"ViewController_iPad" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"]]];

加载图像的代码
- (void) loadImage
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyBundle.bundle/test" ofType:@"png"];
BOOL present = NO;
present = [[NSFileManager defaultManager] fileExistsAtPath:path];
if (present)
{
NSLog(@"MyBundle.bundle/test exists");
}
else
{
NSLog(@"MyBundle.bundle/test does not exists");

}

self.testImageView.image = [UIImage imageNamed:path];

self.testImageView2.image = [UIImage imageWithContentsOfFile:path];
}

MyTestHarnessApp[11671:c07] MyBundle.bundle/test exists

最佳答案

当您使用 imageNamed:您必须只传递一个简单的文件名,并且图像必须存在于应用程序资源包的根目录中。

您的图像不在资源包的根目录中,而是在子文件夹中。

由于您在 path 中获得了图像的完整路径变量,您必须使用 UIImage imageWithContentsOfFile:方法:

self.testImageView.image = [UIImage imageWithContentsOfFile:path];

关于ios - 从 nsbundle 访问图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695506/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com