如何制作iOS资源包?
我在评估的 iOS 项目中看到了一个自定义资源包,所以至少我知道这是可能的。
我的问题是,我对给定图像使用大约 22,000 个图块的 CATiledLayer,并且编译需要很长时间(干净构建需要半小时,常规构建需要 5-10 分钟)。因此,我想获取所有图像并制作一个自定义捆绑包以使其可移植,并且希望不要每次都重新编译到应用程序捆绑包中。
我该怎么办?我检查了文档,但没有看到有关如何实际创建捆绑包的解释。
I saw a custom asset bundle in an iOS project I evaluated, so at least I know it's possible.
My issue is that I'm using a CATiledLayer with about 22,000 tiles for a given image and it takes a very long time to compile (half an hour clean build, 5-10 minutes for regular build). So, I want to take all of the images and make a custom bundle to make it portable and hopefully not recompile into the app bundle each time.
How do I go about this? I checked the docs, but didn't see an explanation on how to actually create the bundle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案非常简单
在finder中创建一个文件夹,向其中添加文件,将其重命名为bundlename.bundle
拖入Xcode - 成功!
访问方式为PathToMainBundle+"/bundlename.bundle"
Answer is stupidly simple
Make a folder in finder, add files to it, rename it to bundlename.bundle
drag into Xcode - success!
to access, use the form of PathToMainBundle+"/bundlename.bundle"
如何创建捆绑包
.bundle
(例如“新建文件夹”->“BundleName.bundle”)PS:您可以随时右键单击该文件夹并点击“显示包内容”以便添加、删除或修改任何文件。
如何将捆绑包添加到 Xcode
如何使用捆绑包
(替换
BundleName
、fileName
和fileType
具有适当的名称)How to create a bundle
.bundle
(e.g. "New folder" -> "BundleName.bundle")PS: You can at any time right click the folder and hit "Show package content" in order to add, remove or modify any of the files.
How to add the bundle to Xcode
How to use the bundle
(Replace
BundleName
,fileName
andfileType
with appropriate names)另外两个有用的建议:
首先,为了在 Xcode 中查看捆绑包的内容,您需要在文件检查器实用程序窗格中将其类型设置为“应用程序捆绑包”。您仍然无法通过 Xcode 进行复制。您需要使用终端,但 Xcode 会立即更新它。
其次,为了使用捆绑包中的资源,这里有一个有用的片段...
正如我在上面的评论中提到的,您不需要实际加载捆绑包(您不能,因为它不可执行)和
ofType 需要与实际文件的大小写匹配才能在设备上运行。它在模拟器中可以以任何方式工作,所以不要被这个转移注意力的东西所迷惑!
最后,您不需要将资源放入捆绑包内的“Resources”子文件夹中。似乎您可以使用任意布局,但可能会有未知的性能影响。
Two other helpful bits of advice:
First, in order to see the contents of the bundle in Xcode you need to set its type in the File Inspector Utility Pane, to "Application Bundle". You still won't be able to copy to and from via Xcode. You'll need to use Terminal but Xcode will update it immediately.
Second, in order to use resources in the bundle here's a helpful snippet...
As mentioned in my comment above, you needn't actually load the bundle (you can't as it's not executable) and the
ofType
needs to match the case of your actual file for it to work on the device. It will work either way in simulator so don't be fooled by this red herring!Finally, you don't need to put your resources in the "Resources" subfolder inside the bundle. It seems you can use an arbitrary layout but there may be unknown performance implications.
这是我如何让它工作的:在 Xcode 中创建一个新文件 |资源 |设置捆绑包。然后在 Finder 中选择该捆绑包并选择“显示包内容”,然后添加任何图像文件。
然后在代码中以这种方式引用图像:
Here's how I got this to work: In Xcode create a new file | Resource | Settings Bundle. Then in the Finder select that bundle and choose Show Package Contents, and add whatever image files.
Then in the code reference an image this way:
以下是创建资产或资源包(例如
FrameworkResources.bundle
)的步骤 - 令人惊讶的是,它并不明显。如果您正在创建静态框架,这尤其有用。SDKROOT
)更改为“iOS”,Here are the steps to create an asset or resource bundle (ex.
FrameworkResources.bundle
) - it's surprisingly non-obvious. This is especially useful if you are creating static frameworks.SDKROOT
) to "iOS'创建可加载捆绑项目就像创建应用程序一样 - 您只需选择适当的项目模板即可。要创建可加载捆绑项目,请执行以下步骤:
构建并运行,在 Xcode 中,您将在 Project Navigator 的 Products 文件夹中看到捆绑文件。右键单击该捆绑包并选择“在 Finder 中显示”。
Creating a loadable bundle project is just like creating an application—you just need to pick the appropriate project template. To create a loadable bundle project, perform the following steps:
Build and run, in Xcode you will see the bundle file in your Products folder of Project Navigator. Right-click the bundle and select "Show in Finder".
我关于在 Xcode 项目中捆绑和读取文件的笔记
步骤:
print(Bundle.main.resourcePath!+"/temp.bundle/test.txt")
输出:/Users/James/Library/Developer/Xcode/DerivedData/GitSyncMac-heiwpdjbtaxzhiclikjotucjguqu/Build /Products/Debug/GitSyncMacApp.app/Contents/Resources/temp.bundle/test.txt示例:
My notes on bundling and reading files in an Xcode project
Steps:
print(Bundle.main.resourcePath!+"/temp.bundle/test.txt")
Output: /Users/James/Library/Developer/Xcode/DerivedData/GitSyncMac-heiwpdjbtaxzhiclikjotucjguqu/Build/Products/Debug/GitSyncMacApp.app/Contents/Resources/temp.bundle/test.txtExample: