在 Swift 中或在终端构建、创建和修改 Asset Catalogs
此库包含以下能力
Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 swift
编译器中。 AssetLib 支持在受支持的平台上使用它。
设置好 Swift 包后,将 AssetLib 作为依赖项添加到 Package.swift
的 dependencies
值中即可。
dependencies: [
.package(url: "https://github.com/brightdigit/AssetLib.git", .upToNextMajor(from: "0.1.0"))
]
AssetLib 包含一个类型 AssetSpecificationDocument
,它可以被构造、解码、编码等。通常在 Xcode Asset Catalogs 中,这将是 Image Set 或 App Icon Set 中的 Contents.json
文件。 因此,要读取 AssetSpecificationDocument
// read the "Contents.json" for either Image Set or App Icon
let dirURL = let outputDirURL = URL(fileURLWithPath: "ImageSet.imageset", isDirectory: true)
let url = dirURL.appendingPathComponent("Contents.json")
let decoder = JSONDecoder()
let data = try Data(contentsOf: url)
let document = decoder.decode(AssetSpecificationDocument.self, from: data)
AssetSpecificationDocument
包含三个属性:info
、properties
和 images
。 images
属性包含 Image Set 或 App Icon 中使用的每个图像的规范。
AssetLib 为 AssetSpecificationDocument
中的每个图像包含一个类型 AssetSpecification
。 这可以是 Image Set 中的 2x 图像,也可以是 iPad 通知的图像。
为了构建或修改 AssetSpecification
,请使用 AssetSpecificationBuilder
类型
...
let document = decoder.decode(AssetSpecificationDocument.self, from: data)
let newImages = document.images.map {
oldImage in
var builder = AssetSpecificationBuilder(specifications: oldImage)
builder.locale = Locale(identifier: "fr")
return builder.assetSpec()
}
let modifiedDocument = AssetSpecificationDocument(
info: document.info,
images: newImages,
properties: document.properties)
为了保存您的新 AssetSpecificationDocument
,只需使用 JSONEncoder
// save to "Contents.json" for either Image Set or App Icon to work in Xcode
let outputDirURL = let outputDirURL = URL(fileURLWithPath: "NewImageSet.imageset", isDirectory: true)
let outputURL = outputDirURL.appendingPathComponent("Contents.json")
// In order to have it look similar to how Xcode outputs the document
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted]
let data = try encoder.encode(modifiedDocument)
try data.write(to: outputURL)
您可以基于模板以编程方式或在终端中为 Asset Catalog 构建 App Icon。
为了创建 AssetSpecificationDocument
,请创建一个具有您选择的设置的 AppIconTemplate
以及 AppIconTemplateBuilder
。 然后调用:.document(fromTemplate:)
来构建 AssetSpecificationDocument
let template = AppIconTemplate(
devices: [.car, .ipad, .iphone, .mac, .tv, .watch],
specifyGamut: true,
prerendered: true)
let builder = AppIconTemplateBuilder()
let document = builder.document(fromTemplate: template)
AppIconTemplate
具有三个属性,这些属性与 Xcode 中可用的属性相对应
devices
: 可选 Set<AppIconDevice>
此 AppIcon 支持的设备,包括:.car
、.ipad
、.iphone
、.mac
、.tv
、.watch
。 如果为 nil
,则假定支持所有设备。specifyGamut
: 可选,默认值:false 是否为 sRGB 和 P3 广色域颜色空间指定单独的图像。prerendered
: 可选,默认值:false iOS 6.0 的向后兼容属性,指示图标是否包含蒙版和发光效果有关更多详细信息,请查看有关 AppIconTemplate
的文档。
为了创建 AssetSpecificationDocument
,请创建一个具有您选择的设置的 ImageSetTemplate
以及 ImageSetTemplateBuilder
。 然后调用:.document(fromTemplate:)
来构建 AssetSpecificationDocument
let template = ImageSetTemplate(
renderAs: .template,
compression: .gpuOptimizedBest,
preserveVectorData: true,
devices: Set([.universal]),
appearances: [
ValuedAppearance(value: Luminosity.light).eraseToAny(),
ValuedAppearance(value: Luminosity.dark).eraseToAny()
],
scaling: .single,
specifyGamut: true,
direction: [],
specifiedWidthClass: nil,
specifiedHeightClass: nil,
memorySet: [],
graphicFSSet: [],
specifyAWWidth: false,
autoScaling: false,
locales: [],
resourceTags: []
)
let builder = ImageSetTemplateBuilder()
let document = builder.document(fromTemplate: template)
有关更多详细信息,请查看有关 ImageSetTemplate
的文档。
除了 API 之外,您还可以使用 Swift 包中提供的可执行文件来构建 AssetSpecificationDocument
,即 Contents.json 文件
USAGE: assetlibrary <template-file> <output>
ARGUMENTS:
<template-file> JSON template file.
<output> Output directory or file. If this path ends in either
'imageset' or 'appicon', then a directory will be
created with a 'Contents.json' file inside.
Otherwise, it will be the resulting file path.
OPTIONS:
-h, --help Show help information.
只需创建一个 json
文件,其中包含 ImageSetTemplate
或 AppIconTemplate
的相应属性。 Swift 中每个属性对应的 JSON 属性是
模板类型 | Swift 名称 | JSON 名称 |
---|---|---|
AppIcon | devices | devices |
AppIcon | specifyGamut | specify-gamut |
AppIcon | prerendered | pre-rendered |
ImageSet | templateRenderingIntent | template-rendering-intent |
ImageSet | compressionType | compression-type |
ImageSet | preservesVectorRepresentation | preserves-vector-representati |
ImageSet | devices | devices |
ImageSet | appearances | appearances |
ImageSet | scaling | scaling |
ImageSet | displayGamuts | display-gamuts |
ImageSet | languageDirections | language-directions |
ImageSet | widthClass | width-class |
ImageSet | heightClass | height-class |
ImageSet | memorySet | memory-set |
ImageSet | graphicsFeatureSets | graphics-feature-sets |
ImageSet | appleWatchScreens | apple-watch-screens |
ImageSet | autoScaling | auto-scaling |
ImageSet | locales | locales |
ImageSet | onDemandResourceTags | on-demand-resource-tags |
imageset-template.json
{
"template-rendering-intent" : "template",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"appleWatchScreens" : true,
"locales" : ["en", "es", "fr"]
}
用法
$ assetlibrary Example/Templates/imageset-template.json Example/Templates/Assets.xcassets/Template.imageset
appicon-iOS.json
{
"devices" : ["iphone", "ipad"]
}
用法
$ assetlibrary Example/Templates/appicon-iOS.json Example/Templates/Assets.xcassets/AppIcon.appiconset
appicon-gamut.json
{
"specify-gamut" : true
}
用法
$ assetlibrary Example/Templates/appicon-gamut.json Example/Templates/Assets.xcassets/Gamut.appiconset
appicon-devices.json
{
"devices" : ["watch", "iphone", "ipad", "mac", "tv"]
}
用法
$ assetlibrary Example/Templates/appicon-devices.json Example/Templates/Assets.xcassets/Devices.appiconset
AssetLib 在 MIT 许可下发布。 查看 LICENSE 以了解详细信息。