cocoa touch framework assets not visible in App Project

廉价感情. 提交于 2019-12-08 14:39:46

问题


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 ?


回答1:


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




回答2:


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




回答3:


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];



回答4:


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.



来源:https://stackoverflow.com/questions/26782706/cocoa-touch-framework-assets-not-visible-in-app-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!