ios - 当从 Interface Builder 设置 UIImageView 的图像时,该图像是如何加载的?

标签 ios cocoa-touch uiimageview interface-builder nsbundle

当 ImageView 的源是从属性检查器中预先设置时,文件的实际路径何时/如何解析?似乎没有对 NSBundle 的任何调用,但我可能错了。

编辑:我正在尝试调配调用的任何方法(如果可能)以便稍后动态替换 Assets 。

enter image description here

最佳答案

UIImage 初始化器或工厂都没有被调用。
我用调试器(在 iOS 模拟器 7.0.3 上)做了一些研究,发现如下:
1) IB中设置的UIImageView通过-initWithCoder:初始化。
2) 在 initWithCoder: 中调用方法 decodeObjectForKey:。并且 (!) 名为 UIImage 的键包含来自 IB 的图像。此图像通过 ivar 设置为 UIImageView,而不是通过 setImage: setter。
因此,似乎 IB 在编译时将原始图像数据打包到 XIB/Storyboard 中。废话,但事实。
这就是为什么我们不能调配 +imageNamed: 或其他工厂,而应该使用条件代码为 retina4 和 iOS6 设置图像

编辑:

评论显示编译后的IB文件的hexdumping中有png名称。

In fact, looking at the output of "hexdump -C BYZ-38-t0r-view-8bC-Xf-vdC.nib " indicates that the filename of the PNG appears in the compiled file. So, it must be loading the file data via the file name from the same bundle.

但是,它们仍然通过某种内部机制加载,而不是通过 imageNamed:

关于ios - 当从 Interface Builder 设置 UIImageView 的图像时,该图像是如何加载的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908584/

相关文章:

ios - 无法在 Swift 上设置/获取映射对象数组到 userDefaults

ios - UIBarButtonItem 不工作

objective-c - 有没有办法在 Interface Builder/storyboard 中自动调整 UIImageView 的大小?

ios - UIImageView.transform 上的 KVO 不稳定

c++ - 命名空间 'vsnprintf' 中没有名为 'std' 的成员;你是说 'vsprintf' 吗?

ios - 用户界面自动化 : Change location when the app is in background

iphone - 为什么 sizeWithFont :constrainedToSize:lineBreakMode: returning an incorrect size?

iphone - 如何从服务器导入图像并将其排列在 View 中

ios - XCode错误: No provisioned iOS devices are available with a compatible iOS version

objective-c - UIWebView canGoBack 和 canGoForward 总是返回 NO