Logo

AssetLib

SwiftPM Twitter GitHub GitHub issues

actions CircleCI Bitrise

Codecov CodeFactor Grade codebeat badge Code Climate maintainability Code Climate technical debt Code Climate issues

Reviewed by Hound

在 Swift 中或在终端构建、创建和修改 Asset Catalogs

特性

此库包含以下能力

需求

安装

Swift Package Manager

Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 swift 编译器中。 AssetLib 支持在受支持的平台上使用它。

设置好 Swift 包后,将 AssetLib 作为依赖项添加到 Package.swiftdependencies 值中即可。

dependencies: [
    .package(url: "https://github.com/brightdigit/AssetLib.git", .upToNextMajor(from: "0.1.0"))
]

用法

API 文档

Asset Catalog 项目 (例如:App 图标和 Image Sets)

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 包含三个属性:infopropertiesimagesimages 属性包含 Image Set 或 App Icon 中使用的每个图像的规范。

Asset Catalog 图像

AssetLibAssetSpecificationDocument 中的每个图像包含一个类型 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。

App 图标

为了创建 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 中可用的属性相对应

有关更多详细信息,请查看有关 AppIconTemplate 的文档。

Image Sets

为了创建 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 文件,其中包含 ImageSetTemplateAppIconTemplate 的相应属性。 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
示例
Image Set

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
iPhone 和 iPad App 图标

appicon-iOS.json

{
  "devices" : ["iphone", "ipad"]
}

用法

$ assetlibrary Example/Templates/appicon-iOS.json Example/Templates/Assets.xcassets/AppIcon.appiconset
多设备 Image Set (带有显示色域)

appicon-gamut.json

{
  "specify-gamut" : true
}

用法

$ assetlibrary Example/Templates/appicon-gamut.json Example/Templates/Assets.xcassets/Gamut.appiconset
多设备 Image Set (没有 CarPlay)

appicon-devices.json

{
  "devices" : ["watch", "iphone", "ipad", "mac", "tv"]
}

用法

$ assetlibrary Example/Templates/appicon-devices.json Example/Templates/Assets.xcassets/Devices.appiconset

链接

许可

AssetLib 在 MIT 许可下发布。 查看 LICENSE 以了解详细信息。