Swiftpack.co - heestand-xyz/AsyncGraphics as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by heestand-xyz.
heestand-xyz/AsyncGraphics 0.4.2
Edit images and video with async / await in Swift, powered by Metal.
⭐️ 15
🕓 2 weeks ago
iOS macOS tvOS
.package(url: "https://github.com/heestand-xyz/AsyncGraphics.git", from: "0.4.2")

AsyncGraphics

The core value type in AsyncGraphics is a Graphic.
It's like an image, tho it can be used with various async methods.

Documentation

Swift Package

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

Example

import SwiftUI
import AsyncGraphics

struct ContentView: View {
    
    @State private var graphic: Graphic?
    
    var body: some View {
        ZStack {
            if let graphic = graphic {
                GraphicView(graphic: graphic)
            } else {
                ProgressView()
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .task {
            do {
                for await cameraGraphic in try Graphic.camera(.front) {
                    graphic = cameraGraphic
                }
            } catch {
                fatalError()
            }
        }
    }
}

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 Graphics.

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) 

GitHub

link
Stars: 15
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics