3

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
Nurjan
  • 5,889
  • 5
  • 34
  • 54
Raditya Kurnianto
  • 734
  • 1
  • 16
  • 42

3 Answers3

2

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

UIImage *image =  [UIImage imageNamed:@"image name without extension"];
  • Or, if there's a folder in place `UIImage *image = [UIImage imageNamed:@"folder/image_name_without_extension"];` – user1232690 Apr 07 '23 at 15:56
0

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

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

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
Kalyani
  • 392
  • 2
  • 17