Reputation: 6080
I'm creating a custom framework. Previously in the framework we would download an image and use that to display in a button:
NSString *path = [NSString stringWithFormat: @"https://s3.amazonaws.com/assets/home.png"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *homeButton = [[UIImage alloc] initWithData:data];
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: @selector(backToRewardsCenter:)];
However, in reviewing this we determined that this isn't good to have as a synchronous call so I'd like to add this PNG to the framework itself.
I've been able to do this by adding the image and ensuring it's included in the copy bundle resources Build Phase. With this set the image gets added in the universal framework output:
However, when I attempt to add this in code, it doesn't seem to show up. Also, when I add the framework in a project, I don't see the image being included, just the headers:
Here's what I've tried so far:
NSString* path = [NSString stringWithFormat:@"%@/TheoremReachSDK.framework/home.png", [[NSBundle mainBundle] bundlePath]];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
NSBundle *bundle = [NSBundle bundleForClass:[TheoremReach class]];
NSString *path = [bundle pathForResource:@"home" ofType:@"png"];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
UIImage *homeButton = [UIImage imageNamed:@"home.png"];
But none of those display anything. Any idea what I need to do to get the image to display?
Upvotes: 0
Views: 693
Reputation: 89509
I'm guessing NSBundle isn't finding the framework via the call to:
[NSBundle bundleForClass:[TheoremReach class]]
Try giving your framework an explicit bundle ID, e.g.: com.theoremreach.sdk
, clean your projects and then rebuild.
You can then use code like this to fetch and display your image:
NSString *bundleIdentifier = @"com.theoremreach.sdk";
NSBundle *bundle = [NSBundle bundleWithIdentifier:bundleIdentifier];
if(bundle != nil) {
NSString *path = [bundle pathForResource:@"home" ofType:@"png"];
UIImage *homeButtonImage = [[UIImage alloc] initWithContentsOfFile:path];
if(homeButtonImage != nil) {
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: @selector(backToRewardsCenter:)];
} else {
NSLog(@"couldn't find home button image in bundle");
}
} else {
NSLog(@"could not find bundle with identifier %@", bundleIdentifier);
}
Upvotes: 1