20

I've been trying since morning creating a cocoa touch farmework to put my self containing widget inside it, and to allow any app I'm working on to use it in the future.

I've managed to build the project and export .framework file, but all the assets are not showing now. all my Images are inside assets catalog.

And they seem to be exported since the .framework file has (assets.car) file inside it.

I'm currently accessing them using

UIImage* icon = [UIImage imageNamed:@"iconName"];

But it always returns null, Any ideas ?

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • As far as I know if you have a .framework file and want to use assets you need to build a bundle and serve that with the .framework – Andrei Nov 06 '14 at 17:18
  • Have you had a look at [Building Modern Frameworks](http://asciiwwdc.com/2014/sessions/416)? – carlodurso Nov 14 '14 at 19:09
  • @Andrei I did that too, and the assets category isn't accessible also, I begun to feel like something is missing from the tutorials or my SDK is somehow broke. I try to access the NSBundle that contains said assets with its ID and I'm getting null. – Mr.Me Nov 15 '14 at 10:05
  • @carlodurso yes. I've watched the video. and the presenter said there is a bug in the assets. – Mr.Me Nov 15 '14 at 10:06
  • What XCode version do you use? – sarunw Jan 14 '15 at 11:40
  • My problem was that I was running on ios 8.4 but the deployment target of the framework was set to 9.x. Though all the code worked, assets could not be loaded from the framework bundle on 8.x until the deploy target was 8.x. – troppoli Feb 01 '16 at 22:52

4 Answers4

24

You can supply the framework bundle when creating an image. In Swift 2:

class func getMyImage() -> UIImage? {
    let bundle = NSBundle(forClass: self)
    return UIImage(named: "YourImageName", inBundle: bundle, compatibleWithTraitCollection: nil)
}

Swift 3:

class func getMyImage() -> UIImage? {
    let bundle = Bundle(for: type(of: self))
    return UIImage(named: "YourImageName", in: bundle, compatibleWith: nil)
}

Here is a demo project containing a framework with an image and an app that uses it.

https://github.com/marketplacer/AssetFrameworkDemo

Jakub
  • 13,712
  • 17
  • 82
  • 139
Evgenii
  • 36,389
  • 27
  • 134
  • 170
  • 3
    How to it in storyboard or xib not class? – Shreyank Feb 09 '18 at 09:47
  • I have the same question. Whenever trying to leverage framework assets in a storyboard / xib, I get the following error, even though the image shows up in the drop-down: **Could not load the "" image referenced from a nib in the bundle with identifier ""**. Any ideas? – TheNeil Apr 25 '19 at 01:51
0

Please try accessing image in the .framework as below :

[[UIImage alloc] initWithContentsOfFile:@"Test.framework/abc.png"];

I assume you have copied the framework and is part of the App bundle

kondi
  • 27
  • 1
0

I have a static class that helps the framework caller. It has a method called

+ (NSBundle*) bundelForHelper;

The implementation looks like so:

+ (NSBundle*) bundelForHelper{
    return [NSBundle bundleForClass:self];
}

Then in my view controller I import the helper and call this in viewdidload:

UIImage *image = [UIImage imageNamed:@"MyImage"
                            inBundle:[MyHelper bundelForHelper]
                 compatibleWithTraitCollection:self.traitCollection];
Tim Sneed
  • 490
  • 3
  • 10
-1

You can try accessing your images as:

UIImage* icon = [UIImage imageNamed:@"MyLib.framework/iconName"];

Check this question about creating frameworks containing resources.

Also I recommend using CocoaPods instead even if your code is closed source.

Community
  • 1
  • 1
Rivera
  • 10,792
  • 3
  • 58
  • 102