Edit images and video with async / await in Swift, powered by Metal
AsyncGraphics
The core value type in AsyncGraphics is a Graphic
.
It’s like an image, tho it can be used with various async methods.
Swift Package
.package(url: "https://github.com/heestand-xyz/AsyncGraphics", from: "0.3.0")
import AsyncGraphics
Graphic – Content
A Graphic
can be created with static funcs e.g. Graphic.image(named:)
or Graphic.circle(size:radius:center:)
.
Color
Colors are represented with the PixelColor
type.
import PixelColor
to create custom colors with hex values.
static func color(_ color: PixelColor, size: CGSize) async throws -> Graphic
static func color(_ color: PixelColor, size: CGSize) async throws -> Graphic
Example:
import PixelColor
let color: Graphic = try await .color(PixelColor(hex: "#FF8000"),
size: CGSize(width: 1000, height: 1000))
Image
Images are represented with TMImage
.
This is a multi platform type alias to UIImage
and NSImage
.
import TextureMap
for extra multi platform methods like .pngData()
for macOS.
static func image(_ image: TMImage) async throws -> Graphic
static func image(named name: String, in bundle: Bundle = .main) async throws -> Graphic
Example:
let image: Graphic = try await .image(named: "Image")
Circle
static func circle(radius: CGFloat? = nil, center: CGPoint? = nil, color: PixelColor = .white, backgroundColor: PixelColor = .black, size: CGSize) async throws -> Graphic
Example:
let circle: Graphic = try await .circle(radius: 250.0,
center: CGPoint(x: 500.0, y: 500.0),
color: .orange,
backgroundColor: .clear,
size: CGSize(width: 1000.0, height: 1000.0))
Frames
You can import a video to an array of Graphic
s.
static func videoFrames(url: URL) async throws -> [Graphic]
Example:
guard let url: URL = Bundle.main.url(forResource: "Video", withExtension: "mov") else { return }
let frames: [Graphic] = try await .videoFrames(url: url)
Graphic – Effects
A Graphic
can be modified with effect funcs e.g. .inverted()
or .blend(graphic:blendingMode:placement:)
.
Invert
func inverted() async throws -> Graphic
Example:
let inverted: Graphic = try await someGraphic.inverted()
Blend
func blended(with graphic: Graphic, blendingMode: BlendingMode, placement: Placement = .fit) async throws -> Graphic
Example:
let blended: Graphic = try await someGraphic.blended(with: otherGraphic, blendingMode: .multiply)
Displace
func displaced(with graphic: Graphic, offset: CGFloat, origin: PixelColor = .gray, placement: Placement = .fill) async throws -> Graphic
Example:
let displaced: Graphic = try await someGraphic.displaced(with: otherGraphic, offset: 100.0)
Graphic – Export
A Graphic
can be exported to a video with func .video(fps:kbps:format:)
.
Video
func video(fps: Int = 30, kbps: Int = 1_000, format: VideoFormat = .mov) async throws -> Data
Examples:
let frames: [Graphic] = ...
let videoData: Data = try await frames.video(fps: 60, kbps: 750, format: .mp4)
let videoURL: URL = try await frames.video(fps: 24, kbps: 500, format: .mov)