The core value type in AsyncGraphics is a Graphic
.
It's like an image, tho it can be used with various async methods.
.package(url: "https://github.com/heestand-xyz/AsyncGraphics", from: "0.4.2")
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()
}
}
}
}
A Graphic
can be created with static funcs e.g. Graphic.image(named:)
or Graphic.circle(size:radius:center:)
.
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))
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")
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))
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)
A Graphic
can be modified with effect funcs e.g. .inverted()
or .blend(graphic:blendingMode:placement:)
.
func inverted() async throws -> Graphic
Example:
let inverted: Graphic = try await someGraphic.inverted()
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)
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)
A Graphic
can be exported to a video with func .video(fps:kbps:format:)
.
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)
link |
Stars: 15 |
Last commit: 2 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics