Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从Bundle加载图像我得到一个零_Ios_Objective C_Uiimage - Fatal编程技术网

Ios 从Bundle加载图像我得到一个零

Ios 从Bundle加载图像我得到一个零,ios,objective-c,uiimage,Ios,Objective C,Uiimage,当我尝试使用NSBundle从我的项目中获取UIImage时,我得到了一个nil值。 我已将图像放入.xcassests 这是我从bundle加载图像的代码 - (UIImage *)imageRating:(NSString *)imageName { return [UIImage imageNamed:imageName inBundle:[MyClass bundle] compatibleWithTraitCollection:nil]; } 这就是Bundle方法 + (NSB

当我尝试使用NSBundle从我的项目中获取UIImage时,我得到了一个nil值。 我已将图像放入
.xcassests

这是我从bundle加载图像的代码

- (UIImage *)imageRating:(NSString *)imageName
{
  return [UIImage imageNamed:imageName inBundle:[MyClass bundle] compatibleWithTraitCollection:nil];
}
这就是Bundle方法

+ (NSBundle *)bundle
 {
    return [NSBundle bundleForClass:[self class]];
 }

知道我为什么会得到零值吗

我猜资源路径是错误的

您的图像被放置在
.xcassests
中,并最终打包在
.app
文件中,该文件通常用于
主捆绑包中

如果您的项目结构包含一个
框架
,只要您在
框架
中获得当前捆绑包,那么路径就不是主捆绑包下的资源,而是
框架
的捆绑包资源

因为我不知道您的项目结构,下一步是知道打印包的路径,并将其与
主包
路径进行比较

- (UIImage *)imageRating:(NSString *)imageName
{
  NSBundle *bundle = [MyClass bundle];
  NSBundle *mainBundle = [NSBundle mainBundle];
  NSLog(@"bundle: %@\n main bundle: %@",imageBundle);
  return [UIImage imageNamed:imageName inBundle:[MyClass bundle] compatibleWithTraitCollection:nil];
}
如果路径不同,有两种方法

  • 将资源以捆绑包的形式放入
    famework

  • [MyClass bundle]
    更改为
    [NSBundle mainBundle]


  • 我猜资源路径是错误的

    您的图像被放置在
    .xcassests
    中,并最终打包在
    .app
    文件中,该文件通常用于
    主捆绑包中

    如果您的项目结构包含一个
    框架
    ,只要您在
    框架
    中获得当前捆绑包,那么路径就不是主捆绑包下的资源,而是
    框架
    的捆绑包资源

    因为我不知道您的项目结构,下一步是知道打印包的路径,并将其与
    主包
    路径进行比较

    - (UIImage *)imageRating:(NSString *)imageName
    {
      NSBundle *bundle = [MyClass bundle];
      NSBundle *mainBundle = [NSBundle mainBundle];
      NSLog(@"bundle: %@\n main bundle: %@",imageBundle);
      return [UIImage imageNamed:imageName inBundle:[MyClass bundle] compatibleWithTraitCollection:nil];
    }
    
    如果路径不同,有两种方法

  • 将资源以捆绑包的形式放入
    famework

  • [MyClass bundle]
    更改为
    [NSBundle mainBundle]