从 nsbundle 访问图像 [英] Accessing images from nsbundle
问题描述
我有一个框架,它试图从一个包中访问 xib 和图像.我能够很好地访问 xib,但无法在 UIImageView 中加载图像.我什至检查文件是否存在并且控制台输出是...
知道如何加载图像吗?
在框架视图控制器文件中加载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 = [[NSFileManager defaultManager] fileExistsAtPath:path];如果(存在){NSLog(@"MyBundle.bundle/test 存在");}别的{NSLog(@"MyBundle.bundle/test 不存在");}self.testImageView.image = [UIImage imageNamed:path];self.testImageView2.image = [UIImage imageWithContentsOfFile:path];}
<块引用>
MyTestHarnessApp[11671:c07] MyBundle.bundle/test 存在
当您使用 imageNamed:
时,您必须只传递一个简单的文件名,并且该图像必须存在于应用程序资源包的根目录中.
您的图像不在资源包的根目录中,而是在子文件夹中.
既然你已经在path
变量中获得了图片的完整路径,你必须使用UIImage imageWithContentsOfFile:
方法:
self.testImageView.image = [UIImage imageWithContentsOfFile:path];
I have a framework which is trying to access xib's and images from a bundle. I am able to access the xib fine but unable to load the images in UIImageView. I even check if the file exists and the console output is yes...
Any idea how to load the image?
Code to load xib in the framework viewcontroller file
self = [super initWithNibName:@"ViewController_iPad" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"]]];
Code to load images
- (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
When you use imageNamed:
you must pass just a simple filename and the image must exist in the root of the app's resource bundle.
Your image is not in the root of the resource bundle, it is in a subfolder.
Since you have obtained the image's full path in the path
variable, you must use the UIImage imageWithContentsOfFile:
method:
self.testImageView.image = [UIImage imageWithContentsOfFile:path];
这篇关于从 nsbundle 访问图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!