AssetLib
在Swift或终端中构建、创建和修改资源目录
特性
此库包括以下功能
- 以编程方式解析和修改资源目录
- 根据模板在代码或终端中创建资源目录
需求
- iOS 8.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+ / Linux
- Xcode 10.2+
- Swift 5+
安装
CocoaPods
CocoaPods是Cocoa项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用CocoaPods将AssetLib集成到Xcode项目,请在您的Podfile
中指定它。
pod 'AssetLib', '~> 0.1.0'
Swift 包管理器
Swift 包管理器(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"))
]
使用方法
API 文档
资源目录项(即应用图标和图片集)
AssetLib 包含一个类型 AssetSpecificationDocument
,它可以被构建、解码、编码等等。通常在 Xcode 资源目录中,这将是一个图片集或应用图标集中的 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
属性包含图片集或应用图标中使用每个图片的规格。
资源目录图片
AssetLib 包含一个类型 AssetSpecification
,用于每个 AssetSpecificationDocument
中的图片。它可以是图片集中 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)
模板设置
您可以根据模板程序化或在终端中为资源目录创建应用图标。
应用图标
为了创建 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>
支持的设备,包括:.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 | 设备 | 设备 |
AppIcon | specifyGamut | specify-gamut |
AppIcon | prerendered | pre-rendered |
ImageSet | templateRenderingIntent | template-rendering-intent |
ImageSet | compressionType | compression-type |
ImageSet | preservesVectorRepresentation | preserves-vector-representati |
ImageSet | 设备 | 设备 |
ImageSet | 外观 | 外观 |
ImageSet | 缩放 | 缩放 |
ImageSet | displayGamuts | display-gamuts |
ImageSet | 语言方向 | language-directions |
ImageSet | 宽度类别 | width-class |
ImageSet | 高度类别 | height-class |
ImageSet | 内存集 | memory-set |
ImageSet | 图形功能集 | graphics-feature-sets |
ImageSet | appleWatchScreens | apple-watch-screens |
ImageSet | autoScaling | auto-scaling |
ImageSet | 区域 | 区域 |
ImageSet | onDemandResourceTags | on-demand-resource-tags |
示例
图片集
- 用于模板图像
- 带有特定暗黑模式
- 支持英语、西班牙语和法语
- 每个Apple Watch尺寸的独立图像
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应用图标
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
多设备图片集合(不含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。