【问题标题】:Accessing images from nsbundle从 nsbundle 访问图像
【发布时间】:2013-05-22 15:14:30
【问题描述】:

我有一个框架试图从包中访问 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];
}

MyTestHarnessApp[11671:c07] MyBundle.bundle/test 存在

【问题讨论】:

    标签: ios uiimageview nsbundle


    【解决方案1】:

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

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

    由于你已经在path变量中获得了图片的完整路径,所以必须使用UIImage imageWithContentsOfFile:方法:

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

    【讨论】:

    • 试过你的方法..不起作用... MyBundle.bundle 是另一个包含图像的包...2
    • 哪个没用?使用imageWithContentsOfFile: 还是使用其他方式从捆绑包中获取路径?使用imageWithContentsOfFile: 的路径应该可以工作。
    • NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png" inDirectory:@"MyBundle.bundle"]; self.testImageView2.image = [UIImage imageWithContentsOfFile:path];我试过这个......它没有工作......
    • 不,这不是我的建议。使用您的原始代码获取path。如调用fileExistsAtPath: 所示。您需要做的就是将imageNamed:path 替换为imageWithContentsOfFile:path
    • 哎呀......我按照你的建议尝试了......它没有用......我希望你阅读问题的前几行......“我有一个框架正在尝试从包中访问 xib 和图像" NSString *path = [[NSBundle mainBundle] pathForResource:@"MyBundle.bundle/test" ofType:@"png"]; self.testImageView2.image = [UIImage imageWithContentsOfFile:path];
    【解决方案2】:

    如果+[UIImage imageNamed:] 没有正确解析路径,您可以随时使用:

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

    【讨论】:

    • 没有UIImage imageWithContentsOfPath:这样的方法。
    • 看,这就是为什么不从内存中键入方法。那应该是 +[UIImage imageWithContentsOfFile]。自动更正真的让我的记忆受到影响。
    • 嗯 ...好吧,我已经能够重现 imageNamed: 没有正确返回图像的问题,但是 imageWithContentsOfFile: 确实在我的测试应用程序中正确创建了图像 - 你试过检查a) 图像是有效的(如果它不在捆绑包中,它是否有效),并且 b) 您是否检查了指定的名称是否正确,包括区分大小写?
    【解决方案3】:

    您可以使用以下内容:

    self.testImageView.image = [UIImage imageNamed:@"MyBundle.bundle/test.png"];
    

    但是如果你把XIB也放在bundle中,接口似乎是相对于bundle的。

    self.testImageView.image = [UIImage imageNamed:@"test.png"]
    

    【讨论】:

      【解决方案4】:

      尝试一步一步来。

      首先在主包中获取包的路径:

      NSString *myBundlePath=[[NSBundle mainBundle] pathForResource:MyBundle ofType:@"bundle"];
      

      检查捆绑包中是否有任何东西。这更适合双重检查。一旦它工作,您可以选择省略此代码。但仔细检查总是一个好主意。

       NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
       NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil];
       NSLog(@"imageURLS:  %@",allImageURLS);
      

      由于您已经有了捆绑路径,因此将图像名称添加到路径中。或者您可以从上述代码中已有的 url 列表中提取它。

      NSString *myImagePath=[myBundlePath stringByAppendingPathComponent:@"test.png"];
      NSLog(@"myImagePath=%@",myImagePath);
      

      然后,既然你有完整的路径,加载图片

      UIImage *testImage = [UIImage imageWithContentsOfFile:backgroundImagePath];
      

      取自配送应用的代码。

      祝你好运

      提姆

      【讨论】:

        【解决方案5】:

        如果图像已经在主包中,只需使用图像名称:test.png 或简单地测试

        [UIImage imageNamed:@"test.png"];
        [UIImage imageNamed:@"test"];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-01
          • 1970-01-01
          • 2013-08-22
          • 2011-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多