Reputation: 1191
When I try to get a UIImage from my project using NSBundle I get a nil value.
I have the image into .xcassests
This is my code to load image from bundle.
- (UIImage *)imageRating:(NSString *)imageName
{
return [UIImage imageNamed:imageName inBundle:[MyClass bundle] compatibleWithTraitCollection:nil];
}
And this is the Bundle Method.
+ (NSBundle *)bundle
{
return [NSBundle bundleForClass:[self class]];
}
Any idea why I get a nil value.
Upvotes: 3
Views: 833
Reputation: 994
I guess the resource path is wrong.
Your image is placed in .xcassests
and is finally packaged in the .app
file, which is commonly used in main bundle
.
If your project structure contains a framework
, just if you get the current bundle in the framwork
, then the path is not the resource under the main bundle, but the bundle resource of the framwork
.
Because I don't know your project structure, the next step is to know the path to print the bundle and compare it with the main bundle
path.
- (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];
}
If the paths are different, there are two ways.
Put resources in the form of bundles in famework
Change [MyClass bundle]
to [NSBundle mainBundle]
Upvotes: 0