Ios 从Bundle加载图像我得到一个零
当我尝试使用NSBundle从我的项目中获取UIImage时,我得到了一个nil值。 我已将图像放入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
.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]