Impossible to load an image in xcassets on bundle

本文探讨了在iOS静态库中使用xcassets加载图片遇到的问题,并提供了适用于iOS7和iOS8及以后版本的解决方案。对于iOS7,作者提供了一个分类;对于iOS8及更高版本,推荐使用Apple提供的imageNamed:inBundle:compatibleWithTraitCollection:方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I need to include images in a static library. I created a bundle and inserted in my images, the problem is that it seems to work if I include the images directly in the bundle, but stops working if I put in a xcassets file.

I followed many guides and searched for a solution on this site. The most popular solution is to insert this line of code:

[UIImage imageNamed:@"MyBundle.bundle/imageName"] 

but it seems not work for me

any ideas?

share editflag
 
   
  
facing same issue, Did you able to fix this? – BaSha Feb 26 '15 at 7:07
   
  
@BaSha it is possible using iOs 8 with this method: + (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle compatibleWithTraitCollection:(UITraitCollection *)traitCollection; With iOs 7 the best solution is remove the images from the xcassets file – Serluca Feb 26 '15 at 9:38 
   
  
thanks, though I had to add images separately in bundle as iOS 7 support was required – BaSha Feb 26 '15 at 9:44
2 
  
@BaSha I created this category gist.github.com/serluca/e4f6a47ffbc19fccc63e . In this way, you can use after: [NSBundle imageNamed:@"imageName"]; – Serluca Feb 26 '15 at 10:20

4 Answers

up vote 7down voteaccepted

There are two ways to solve this,

If your app is still supporting iOs 7, you can use this category:https://gist.github.com/serluca/e4f6a47ffbc19fccc63e

Otherwise, starting from iOs 8 Apple added a way to do this using: + imageNamed:inBundle:compatibleWithTraitCollection: defined here

share editflag
 
   
  
know of any way to refer to this inside Interface Builder? – jowie Dec 3 '15 at 11:09
1 
  
@jowie did you try with this stackoverflow.com/a/7733614/1728552 – Serluca Dec 3 '15 at 12:38
   
  
I did but that refers to a png, rather than an .xcassets identifier. Having said that I didn't realise but my problem goes a little deeper because it's not importing assets from my custom framework. I will try it again after (hopefully) I out out my other issue. Thanks! – jowie Dec 3 '15 at 15:05 
   
  
imageNamed:inBundle:compatibleWithTraitCollection: works with a PNG embedded in the bundle flat, but once you put it in the .xcassets folder, I cannot find a way of referring to it anymore... – jowie Dec 3 '15 at 15:35

Running the same problem. Looks like inline bundle support is broken for XCAssets in the 1-parameter imageNamed method. There's a workaround though using imageNamed:inBundle:compatibleWithTraitCollection: Be careful, this is iOS8 only !!

NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"static_lib_bundle_name" ofType:@"bundle"]];
UIImage *image  = [UIImage imageNamed:@"image_in_the_xcassets_you_want" inBundle:bundle compatibleWithTraitCollection:nil];

NOTE : traitCollection is set to nil to pass the main screen traits as per apple docs (i don't quite get what it means though, if anyone knows please comment!).

share editflag
 
   
  
any good way to do it in iOS7? – user1010819 Dec 20 '14 at 12:56
2 
  
So I've been having the same issue, looks like the scenario is that xcode compile xcassets directly to a binary file (.car) and copies into main bundle, which mean xcassets cannot be contained in a bundle resource. devforums.apple.com/message/968859#968859 – Adil Soomro Feb 6 '15 at 11:49

Our images are placed in Images.xcassets and we had a problem with loading images in an IBDesignable. The following code did the job for the preview in Interface builder and the app as well:

NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UIImage* image = [UIImage imageNamed:@"image.jpg" inBundle:bundle compatibleWithTraitCollection:nil];
share editflag
 
   
  
As I said this method is available on starting from iOs 8. If you use iOs 7 you can use my category insteadgist.github.com/serluca/e4f6a47ffbc19fccc63e – Serluca May 1 '15 at 13:09

For Swift 2.1:

let bundle = pathToBundle // define for your app or framework
if let image = UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil) {
    // process image
}
share editflag

转载于:https://www.cnblogs.com/ioriwellings/p/5363695.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值