如何制作iOS资源包?

发布于 2024-10-15 21:16:42 字数 227 浏览 7 评论 0原文

我在评估的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

调妓 2024-10-22 21:16:42

答案非常简单

在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"

飘然心甜 2024-10-22 21:16:42

如何创建捆绑包

  1. 在 Finder 中创建一个文件夹。
  2. 添加文件到文件夹
  3. 重命名文件夹,使其扩展名为.bundle(例如“新建文件夹”->“BundleName.bundle”)

PS:您可以随时右键单击该文件夹并点击“显示包内容”以便添加、删除或修改任何文件。

如何将捆绑包添加到 Xcode

  1. 将其拖入 Xcode

如何使用捆绑包

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; 
NSString *resource = [bundle pathForResource:@"fileName" ofType:@"fileType"];

(替换 BundleNamefileNamefileType 具有适当的名称)

How to create a bundle

  1. Create a folder in finder.
  2. Add files to the folder
  3. Rename the folder so that its extension is .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

  1. Drag it into Xcode

How to use the bundle

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; 
NSString *resource = [bundle pathForResource:@"fileName" ofType:@"fileType"];

(Replace BundleName, fileName and fileType with appropriate names)

幻想少年梦 2024-10-22 21:16:42

另外两个有用的建议:

首先,为了在 Xcode 中查看捆绑包的内容,您需要在文件检查器实用程序窗格中将其类型设置为“应用程序捆绑包”。您仍然无法通过 Xcode 进行复制。您需要使用终端,但 Xcode 会立即更新它。

其次,为了使用捆绑包中的资源,这里有一个有用的片段...

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"AquarianHarp" ofType:@"bundle"];
NSString *imageName = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"trebleclef2" ofType:@"png"];
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:imageName];

正如我在上面的评论中提到的,您不需要实际加载捆绑包(您不能,因为它不可执行)和 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...

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"AquarianHarp" ofType:@"bundle"];
NSString *imageName = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"trebleclef2" ofType:@"png"];
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:imageName];

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.

鸵鸟症 2024-10-22 21:16:42

这是我如何让它工作的:在 Xcode 中创建一个新文件 |资源 |设置捆绑包。然后在 Finder 中选择该捆绑包并选择“显示包内容”,然后添加任何图像文件。

然后在代码中以这种方式引用图像:

NSString *imgName = @"bundlename.bundle/my-image.png";
UIImage *myImage = [UIImage imageNamed:imgName]; 

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:

NSString *imgName = @"bundlename.bundle/my-image.png";
UIImage *myImage = [UIImage imageNamed:imgName]; 
被你宠の有点坏 2024-10-22 21:16:42

以下是创建资产或资源包(例如FrameworkResources.bundle)的步骤 - 令人惊讶的是,它并不明显。如果您正在创建静态框架,这尤其有用。

  1. 按文件->新-> Xcode 中的目标
  2. 选择“macOS”选项卡,搜索“Bundle”
  3. 点击“Bundle”->点击“下一步”->输入产品名称“MyResources”->点击“完成”
  4. 转到新创建的捆绑包的“构建设置”。将“Base SDK”(SDKROOT)更改为“iOS”,
  5. 转到新创建的捆绑包的“构建阶段”。删除“编译源”和“链接二进制文件与库”(这将删除其中的可执行文件)可能导致各种构建和应用程序提交错误的捆绑包)

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.

  1. Press File -> New -> Target in Xcode
  2. Select "macOS" tab, search "Bundle"
  3. Tap "Bundle" -> click "Next" -> type product name "MyResources" -> tap "Finish"
  4. Go to "Build Settings" for the newly created Bundle. Change "Base SDK" (SDKROOT) to "iOS'
  5. Go to "Build Phases" for the newly created Bundle. Delete "Compile Sources" and "Link Binary With Libraries" (this will remove the executable within the bundle which can cause all sorts of build and app submission errors)
无言温柔 2024-10-22 21:16:42

创建可加载捆绑项目就像创建应用程序一样 - 您只需选择适当的项目模板即可。要创建可加载捆绑项目,请执行以下步骤:

  1. 启动 Xcode。
  2. 从“文件”菜单中选择“新建项目...”。
  3. 从模板列表中,选择 Cocoa Bundle。
  4. 单击“下一步”。
  5. 选择项目的位置并单击完成。

构建并运行,在 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:

  1. Launch Xcode.
  2. Choose New Project… from the File menu.
  3. From the template list, select Cocoa Bundle.
  4. Click Next.
  5. Choose the location for the project and click Finish.

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".

翻了热茶 2024-10-22 21:16:42

我关于在 Xcode 项目中捆绑和读取文件的笔记

步骤:

  1. 创建一个 test.txt 文件并向其中添加文本“testing”,然后将其放入名为 test.bundle 的文件夹中
  2. 将其拖放到 Xcode 中的 .app 文件旁边(复制)
  3. 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

示例:

print(content(Bundle.main.resourcePath!+"/temp.bundle/test.txt")) // testing
static func content(_ path:String)->String?{
    do {
        let content = try String(contentsOfFile:path, encoding:String.Encoding.utf8) as String//encoding: NSUTF8StringEncoding
        return content
    } catch {
        return nil
    }
}

My notes on bundling and reading files in an Xcode project

Steps:

  1. Create a test.txt file and add the text "testing" to it then put it in a folder named test.bundle
  2. Drag and drop it next to your .app file in Xcode (copy)
  3. 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.txt

Example:

print(content(Bundle.main.resourcePath!+"/temp.bundle/test.txt")) // testing
static func content(_ path:String)->String?{
    do {
        let content = try String(contentsOfFile:path, encoding:String.Encoding.utf8) as String//encoding: NSUTF8StringEncoding
        return content
    } catch {
        return nil
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文