我有一个框架,它试图从一个包中访问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 = 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];
}MyTestHarnessApp11671:c07 MyBundle.bundle/test存在
发布于 2013-05-22 16:01:47
当您使用imageNamed:时,您必须只传递一个简单的文件名,并且图像必须存在于应用程序资源包的根目录中。
您的映像不在资源包的根中,而是在子文件夹中。
由于您已经在path变量中获得了图像的完整路径,所以必须使用UIImage imageWithContentsOfFile:方法:
self.testImageView.image = [UIImage imageWithContentsOfFile:path];发布于 2013-05-22 15:46:07
如果+[UIImage imageNamed:]没有正确解析路径,则始终可以使用:
self.testImageView.image = [UIImage imageWithContentsOfFile: path];发布于 2013-05-22 15:23:34
您只需使用以下内容:
self.testImageView.image = [UIImage imageNamed:@"MyBundle.bundle/test.png"];但是,如果您也将XIB放在包中,则接口将相对于包。
self.testImageView.image = [UIImage imageNamed:@"test.png"]https://stackoverflow.com/questions/16695506
复制相似问题