在 Swift 中使用 async / await 编辑图像和视频,由 Metal 提供支持

异步图形

AsyncGraphics 中的核心值类型是 . 它就像一个图像,它可以与各种异步方法一起使用。Graphic

文档

斯威夫特套餐

.package(url: "https://github.com/heestand-xyz/AsyncGraphics", from: "0.3.0")
import AsyncGraphics

图形 – 内容

可以使用静态函数创建 A,例如 或。GraphicGraphic.image(named:)Graphic.circle(size:radius:center:)

颜色

颜色用类型表示。 以创建具有十六进制值的自定义颜色。PixelColorimport PixelColor

static func color(_ color: PixelColor, size: CGSize) async throws -> Graphic
static func color(_ color: PixelColor, size: CGSize) async throws -> Graphic

例:

import PixelColor

let color: Graphic = try await .color(PixelColor(hex: "#FF8000"), 
                                      size: CGSize(width: 1000, height: 1000))

Image

Images are represented with . This is a multi platform type alias to and . for extra multi platform methods like for macOS.TMImageUIImageNSImageimport TextureMap.pngData()

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 s.Graphic

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 can be modified with effect funcs e.g. or .Graphic.inverted().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 can be exported to a video with func .Graphic.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) 

GitHub

点击跳转