Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Cocoapod library and [UIImage imageNamed:] returns NULL

I have https://github.com/fulldecent/FDChessBoardView working great and am now starting the project again from scratch with pod lib create in order to make a Podspec.

There is one {'.h','.m'} file and some images. The images are in the file system in the provided Pod/Assets folder. The resources are noted in the podspec file with:

s.resource_bundles = {
  'FDChessboardView' => ['Pod/Assets/*']
}

(I have also tried directly adding these files into the Development Pods/FDChessboardView/Resources group inside XCode.)

Inside the library implementation file I need to refer to these images. I have tried:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"FDChessboardView" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *imagePath = [bundle pathForResource:@"aa" ofType:@"png"];
UIImage* image = [UIImage imageWithContentsOfFile:imagePath];

Here the imagePath is set correctly. This file exists and file confirms it is a PNG:

[...]aa.png: PNG image data, 182 x 164, 8-bit/color RGBA, non-interlaced

However the UIImage is NULL.

I have also tried these:

image = [UIImage imageNamed:@"aa"];
UIImage *image = [UIImage imageNamed:@"FDChessboardView.bundle/aa.png"];
image = [UIImage imageNamed:@"aa" inBundle:bundle compatibleWithTraitCollection:nil];

All of them produce NULL.

Could anyone help point me in the correct direction for how to load theme image assets?

Thank you!

Will

like image 252
William Entriken Avatar asked Sep 14 '14 17:09

William Entriken


People also ask

How do you make a Cocoapod framework in Swift?

Create a Cocoa Touch Framework Xcode projectLaunch Xcode and create a new project, choose Cocoa Touch Framework . Enter the name SwiftyLib , select the checkbox Include Unit Tests . On the next page select the project location and do not check Create Git repository on my Mac as we are going to create it later on.


2 Answers

Ok - I found a solution that works for me:

In the lib I am creating, I have the following class: AEBubbleField.h AEBubbleView.m

I also have an image in the Assets folder of the generated file structure called background.png. I want to use this in my lib files above so I add the following to the pod spec (to ensure the png is copied with my lib classes)

s.ios.resource_bundle = { 'AEBubbleField' => 'Pod/Assets/*.png' }

I can then access the image in the following manner:

NSBundle *bundle = [NSBundle bundleForClass:[self class]]; // Any class in the lib can be used here, I just went for the current class
UIImage *backgroundImage = [UIImage imageNamed:@"background.png" inBundle:bundle compatibleWithTraitCollection:nil];

This is the only way I can get the image and use it. All other methods I have tried simply return nil like the original question's.

like image 128
Rob Avatar answered Sep 27 '22 22:09

Rob


this answer works for me.
https://stackoverflow.com/a/34324540/1897767

and, my swift code is:

class func loadImage(name: String) -> UIImage? {
    let podBundle = NSBundle(forClass: MyClass.self)
    if let url = podBundle.URLForResource("MyBundleName", withExtension: "bundle") {
        let bundle = NSBundle(URL: url)
        return UIImage(named: name, inBundle: bundle, compatibleWithTraitCollection: nil)
    }
    return nil
}
like image 38
Daishi Nakajima Avatar answered Sep 27 '22 23:09

Daishi Nakajima