Retrieving image from asset folder - iOS

2.9k views Asked by At

I'm writing a class to build a cocoapods and I want to get the image I placed in Assets.xcassets. The problem is always getting nil image. Are there anything I miss?

Here is my podspec file : enter image description here

Here is the directory stucture : enter image description here

This is what I try to load the Image:

#import "HaloImage.h"

@implementation HaloImage
- (void)showImage {
    NSBundle * bundle = [NSBundle bundleForClass:self];
    NSURL * bundleUrl = [bundle URLForResource:@"Assets" withExtension:@"bundle"];
    self.image = [UIImage imageNamed:bundleUrl.absoluteString];
}
@end
3

There are 3 answers

1
Mohammad Arifuzzaman On

you can use image from asset or main bundle only by name (png file)

self.image = [UIImage imageNamed:@"name_of_image"];
0
Kalyani On

Can you please try below code :

@implementation HaloImage
- (void)showImage {
    NSBundle * bundle = [NSBundle bundleForClass:self];
    NSURL * bundleUrl = [bundle pathForResource:@"Assets" withExtension:@"bundle"];
    self.image = [UIImage imageNamed:bundleUrl.absoluteString];
}
@end
1
Dinesh Kumar Vyas On

You can access image from Assets.xcassets by this. Give it a try

UIImage *image =  [UIImage imageNamed:@"image name without extension"];