The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. AssetLib does support its use on supported platforms.
Once you have your Swift package set up, adding AssetLib as a dependency is as easy as adding it to the dependencies value of your Package.swift.
Asset Catalog Items (i.e. App Icons and Image Sets)
AssetLib contains a type AssetSpecificationDocument which can both be constructed, decoded, encoded and more. Typically in Xcode Asset Catalogs, this would be the Contents.json file inside an Image set or App Icon set. Therefore to read an 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 contains three properties: info, properties, and images. The images property contains the specifications for each image used in the Image set or App Icon.
Asset Catalog Images
AssetLib contains a type AssetSpecification for each image in a AssetSpecificationDocument. This could be anything from a 2x image in an Image Set to the image for an iPad notification.
In order to build or modify an AssetSpecification use the AssetSpecificationBuilder type:
...
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)
Saving Your Document
In order to save your new AssetSpecificationDocument, simply use 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)
Templating
You can build an App Icon for an Asset Catalog based on a template either programmatically or in the terminal.
App Icons
In order to create an AssetSpecificationDocument, create an AppIconTemplate with the settings of your choice as well as a AppIconTemplateBuilder. Then call: .document(fromTemplate:) to build the 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 has three properties which correspond with the properties in available in Xcode:
devices: optionalSet<AppIconDevice> the devices which this AppIcon supports, which are: .car, .ipad, .iphone, .mac, .tv, .watch. If nil, assume all devices are supported.
specifyGamut: optional, default: false whether to specify separate images for sRGB and the P3 wide gamut color space.
prerendered : optional, default: false backward compatibile property for iOS 6.0 indicating if the icon includes the mask and shine effect
For more details, check out the documentation on AppIconTemplate.
Image Sets
In order to create an AssetSpecificationDocument, create an ImageSetTemplate with the settings of your choice as well as a ImageSetTemplateBuilder. Then call: .document(fromTemplate:) to build the AssetSpecificationDocument:
For more details, check out the documentation on ImageSetTemplate.
Command Line Application
In addition to the API, you can build an AssetSpecificationDocument i.e. Contents.json file using the executable provided in the Swift package:
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.
Simply create a json file, with the corresponding properties for a the ImageSetTemplate or AppIconTemplate. The corresponding JSON properties for each property in Swift are:
AssetLib
Features
Included with this library is the ability:
Reqirements
Installing
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the
swift
compiler. AssetLib does support its use on supported platforms.Once you have your Swift package set up, adding AssetLib as a dependency is as easy as adding it to the
dependencies
value of yourPackage.swift
.Usage
API Documentation
Asset Catalog Items (i.e. App Icons and Image Sets)
AssetLib contains a type
AssetSpecificationDocument
which can both be constructed, decoded, encoded and more. Typically in Xcode Asset Catalogs, this would be theContents.json
file inside an Image set or App Icon set. Therefore to read anAssetSpecificationDocument
:AssetSpecificationDocument
contains three properties:info
,properties
, andimages
. Theimages
property contains the specifications for each image used in the Image set or App Icon.Asset Catalog Images
AssetLib contains a type
AssetSpecification
for each image in aAssetSpecificationDocument
. This could be anything from a 2x image in an Image Set to the image for an iPad notification.In order to build or modify an
AssetSpecification
use theAssetSpecificationBuilder
type:Saving Your Document
In order to save your new
AssetSpecificationDocument
, simply useJSONEncoder
:Templating
You can build an App Icon for an Asset Catalog based on a template either programmatically or in the terminal.
App Icons
In order to create an
AssetSpecificationDocument
, create anAppIconTemplate
with the settings of your choice as well as aAppIconTemplateBuilder
. Then call:.document(fromTemplate:)
to build theAssetSpecificationDocument
:AppIconTemplate
has three properties which correspond with the properties in available in Xcode:devices
: optionalSet<AppIconDevice>
the devices which this AppIcon supports, which are:.car
,.ipad
,.iphone
,.mac
,.tv
,.watch
. Ifnil
, assume all devices are supported.specifyGamut
: optional, default: false whether to specify separate images for sRGB and the P3 wide gamut color space.prerendered
: optional, default: false backward compatibile property for iOS 6.0 indicating if the icon includes the mask and shine effectFor more details, check out the documentation on
AppIconTemplate
.Image Sets
In order to create an
AssetSpecificationDocument
, create anImageSetTemplate
with the settings of your choice as well as aImageSetTemplateBuilder
. Then call:.document(fromTemplate:)
to build theAssetSpecificationDocument
:For more details, check out the documentation on
ImageSetTemplate
.Command Line Application
In addition to the API, you can build an
AssetSpecificationDocument
i.e. Contents.json file using the executable provided in the Swift package:Simply create a
json
file, with the corresponding properties for a theImageSetTemplate
orAppIconTemplate
. The corresponding JSON properties for each property in Swift are:Examples
Image Set
imageset-template.json
usage
iPhone and iPad App Icon
appicon-iOS.json
usage
Multi-Device Image Set (w/ Display Gamuts)
appicon-gamut.json
usage
Multi-Device Image Set (w/o CarPlay)
appicon-devices.json
usage
Links
License
AssetLib is released under the MIT license. See LICENSE for details.