如何制作 iOS 资产包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4888208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:46:48  来源:igfitidea点击:

How to make an iOS asset bundle?

iosbundleassets

提问by michael

I saw a custom asset bundle in an iOS project I evaluated, so at least I know it's possible.

我在评估的 iOS 项目中看到了一个自定义资产包,所以至少我知道这是可能的。

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.

我的问题是我对给定的图像使用了大约 22,000 个图块的 CATiledLayer,编译需要很长时间(干净构建半小时,常规构建需要 5-10 分钟)。所以,我想获取所有图像并制作一个自定义包以使其可移植,并希望每次都不会重新编译到应用程序包中。

How do I go about this? I checked the docs, but didn't see an explanation on how to actually create the bundle.

我该怎么做?我检查了文档,但没有看到有关如何实际创建包的解释。

回答by michael

Answer is stupidly simple

答案很简单

Make a folder in finder, add files to it, rename it to bundlename.bundle

在finder中创建一个文件夹,向其中添加文件,将其重命名为bundlename.bundle

drag into Xcode - success!

拖入 Xcode - 成功!

to access, use the form of PathToMainBundle+"/bundlename.bundle"

访问,使用PathToMainBundle+"/bundlename.bundle"的形式

回答by hfossli

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")
  1. 在finder中创建一个文件夹。
  2. 将文件添加到文件夹
  3. 重命名文件夹,使其扩展名是.bundle(例如“新文件夹”->“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.

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

How to add the bundle to Xcode

如何将包添加到 Xcode

  1. Drag it into Xcode
  1. 将其拖入 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, fileNameand fileTypewith appropriate names)

(替换BundleName,fileNamefileType使用适当的名称)

回答by Hari Karam Singh

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.

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

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 ofTypeneeds 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!

正如我在上面的评论中提到的,您实际上不需要加载包(您不能加载,因为它不可执行)并且ofType需要匹配您的实际文件的大小写以使其在设备上工作。它可以在模拟器中以任何方式工作,所以不要被这个红鲱鱼所迷惑!

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.

最后,您不需要将资源放在包内的“Resources”子文件夹中。似乎您可以使用任意布局,但可能存在未知的性能影响。

回答by LandedGently

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.

这是我如何让它工作的:在 XCode 中创建一个新文件 | 资源 | 设置包。然后在 Finder 中选择该包并选择 Show Package Contents,并添加任何图像文件。

Then in the code reference an image this way:

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

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

回答by eonist

My notes on bundling and reading files in an XCode project

我在 XCode 项目中打包和读取文件的笔记

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

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
    }
}

回答by kgaidis

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.

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

  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)
  1. 在 Xcode 中按 File -> New -> Target
  2. 选择“macOS”选项卡,搜索“捆绑”
  3. 点击“捆绑包”-> 点击“下一步”-> 输入产品名称“MyResources”-> 点击“完成”
  4. 转到新创建的捆绑包的“构建设置”。将“基础 SDK” ( SDKROOT)更改为“iOS”
  5. 转到新创建的捆绑包的“构建阶段”。删除“Compile Sources”和“Link Binary With Libraries”(这将删除捆绑包中的可执行文件,这会导致各种构建和应用程序提交错误)

回答by DàChún

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:

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

  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.
  1. 启动 Xcode。
  2. 从文件菜单中选择新建项目...。
  3. 从模板列表中,选择 Cocoa Bundle。
  4. 点击下一步。
  5. 选择项目的位置,然后单击完成。

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 中,您将在 Project Navigator 的 Products 文件夹中看到捆绑文件。右键单击捆绑包并选择“在 Finder 中显示”。