I am developing an iOS framework which includes image resources, I call the methods below in the framwork,
crossImage = [UIImage imageNamed:@"cross"];
arrowImage = [UIImage imageNamed:@"arrow"];
and then I build a demo to test my framework, but find crossImage and arrowImage are both nil; Afterwards, I figure out imageNamed:
method
will search images in the app's directory not in the framework's, so I can fix it by adding the two images to the demo project. However, it's barely elegant. so any other solutions to target the images in my framework?
What is XCFramework? Apple defines XCFrameworks as a distributable binary package created by Xcode that contains variants of a framework or library so that it can be used on multiple platforms (iOS, macOS, tvOS, and watchOS), including Simulator builds.
What is a Framework? Frameworks are self-contained, reusable chunks of code and resources you can import into many apps. You can even share them across iOS, tvOS, watchOS and macOS apps. When combined with Swift's access control, frameworks help define strong, testable interfaces between code modules.
In the app, select the project from the project navigator, select the Stocktance target, and scroll to Frameworks, Libraries, and Embedded Content. Click on the plus button, click Add Other… and select Add Files… Navigate to the SettingsKit folder and select it. We've added the framework to the project.
An object that manages image data in your app.
You can load the image from framework using:
+ (UIImage *)imageNamed:(NSString *)name
inBundle:(NSBundle *)bundle
compatibleWithTraitCollection:(UITraitCollection *)traitCollection
method.
In your framework class write like:
NSBundle *frameWorkBundle = [NSBundle bundleForClass:[self class]];
UIImage *arrow = [UIImage imageNamed:@"arrow" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
UIImage *cross = [UIImage imageNamed:@"cross" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
Refer UIImage Class Reference for more info about this method.
You can also load images using:
[[UIImage alloc] initWithContentsOfFile:path];
The path you'll want to generate from the bundle path. Something like:
NSBundle *bundle = [NSBundle bundleForClass:[YourFramework class]];
NSString* path = [bundle pathForResource:@"cross.jpg" ofType:nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With