使用 iOS 从捆绑包中加载图像
我在项目中添加了一个 .bundle 文件夹,其中填充了一些图像。 直接引用此图像是否正确,写类似?:
[UIImage imageNamed:@"imageInBundle.png"];
访问和使用这些图像的最佳方法是什么?
I added to my project a .bundle
folder filled with some images.
Is it correct refer to this image directly writing something like ?:
[UIImage imageNamed:@"imageInBundle.png"];
Which is the best method to access and use these images ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
是的,
imageNamed:
将搜索您的主包。项目中的图像,即使它们位于项目导航器中的不同组中,也将位于您的主包中,并且可以通过名称直接访问。That is correct,
imageNamed:
will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.如果这不起作用,请尝试
最佳
If that does not work, try
Best
值得庆幸的是,从 iOS 8 开始,Apple 已经为此提供了适当的 API,
imageNamed:inBundle:companyWithTraitCollection:
。它的使用方式如下:
或在 swift:
或在 swift 5 中:
Apple has thankfully provided a proper API for this from iOS 8 onwards,
imageNamed:inBundle:compatibleWithTraitCollection:
.It's used like so:
or in swift:
or in swift 5:
1) 如果您正在使用捆绑包,请转到捆绑包目标并将 COMBINE_HIDPI_IMAGES 设置为 NO。
在另一种情况下,图像将转换为 tiff 格式。
2)尝试这个代码:
1) If you are working with bundles go to bundle target and set COMBINE_HIDPI_IMAGES to NO.
In another case the image will be converted to tiff format.
2) try this code:
Swift 3
我无法获取当前捆绑包,所以我这样做:
Swift 3
I can't get current bundle so i do this:
您需要执行以下步骤:
为此,请按照下列步骤操作: 在 Xcode 中,转到“文件”->“文件”。新->
目标。在模板选择对话框中选择“iOS”或“macOS”。
向下滚动并从模板列表中选择“捆绑包”。点击
“下一步”并为您的包命名(例如“ImageResourcesBundle”)。
图像文件到捆绑包:
捆绑包的目标。为此,请在 Xcode 项目中选择捆绑包
导航器,然后将图像文件拖放到包中
“复制捆绑资源”构建阶段。
使用适当的 Bundle 方法从资源包中获取图像。
下面是如何执行此操作的示例:
**
Objective C 代码
然后您可以调用此函数从资源包中获取图像:
You'll need to follow these steps:
To do this, follow these steps: In Xcode, go to File -> New ->
Target. Select "iOS" or "macOS" in the template selection dialog.
Scroll down and choose "Bundle" from the list of templates. Click
"Next" and give your bundle a name (e.g., "ImageResourcesBundle").
image files to the bundle:
bundle's target. To do this, select the bundle in Xcode's project
navigator, and then drag and drop the image files into the bundle's
"Copy Bundle Resources" build phase.
image from the resource bundle using the appropriate Bundle method.
Below is an example of how to do it:
**
Objective C Code
You can then call this function to get the image from the resource bundle:
Swift 版本
@AndrewMackenzie 的回答对我不起作用。这就是有效的方法
我的完整答案在这里。
Swift version
@AndrewMackenzie's answer didn't work me. This is what did work
My full answer is here.
由于我必须为 Swift 解决这个问题,我想我还要添加......
我的包内的
Supporting Files
组中有TestImage.png
。Since I had to figure this out for Swift, thought I would add also....
Where I have
TestImage.png
in theSupporting Files
group inside my bundle.对于斯威夫特 3
For Swift 3
如果您要在同一个 viewController 中多次使用它,有一种快速方法:
在与以下方法相同的类中的任何位置获取图像:
获取图像的方法:
或者直接从捆绑包中调用它:
A quick way to do it if you are going to use it a couple of times in the same viewController:
Get the image anywhere in the same class as the method below:
Method that fetches the image:
Or simply call it straight out of the bundle:
如果您正在构建一个框架,并且希望
UIImage(named: "image")
的所有实例始终使用您的自定义包,您可以创建一个如下所示的扩展来覆盖它If you are building a framework and you would like all instances of
UIImage(named: "image")
to always use your custom bundle, you can make an extension like below that will override it我在尝试解决从 Swift Package 加载图像时发现了这个问题,所以如果其他人也遇到这个问题,这就是我解决它的方法:
let image = UIImage(named: "imageName", in: Bundle.module,兼容:无)
I found this question when trying to solve loading images from Swift Package, so if anybody else is struggling with that as well here's how I solved it:
let image = UIImage(named: "imageName", in: Bundle.module, compatibleWith: nil)
这在 Swift 5 中对我有用
This is worked for me in Swift 5