Raditya Kurnianto
Raditya Kurnianto

Reputation: 744

Retrieving image from asset folder - iOS

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

Upvotes: 3

Views: 3228

Answers (3)

Dinesh Kumar Vyas
Dinesh Kumar Vyas

Reputation: 482

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

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

Upvotes: 2

Kalyani
Kalyani

Reputation: 392

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

Upvotes: 0

Mohammad Arifuzzaman
Mohammad Arifuzzaman

Reputation: 878

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

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

Upvotes: 0

Related Questions