SwiftarrImage
public class SwiftarrImage
Image wrapper backed by libvips. Manages a VipsImage pointer with automatic cleanup. Operations return new instances — vips images are immutable.
-
The underlying libvips image pointer.
Declaration
Swift
private var vipsImage: UnsafeMutablePointer<VipsImage> -
Retains the encoded source buffer for images loaded via
init(data:). libvips loads encoded buffers lazily and reads pixel data on demand, so the buffer must outlive the image. Holding it here ties its lifetime to this instance instead of the caller’s localData. Nil for images created from owned pixel data or derived from operations.Declaration
Swift
private let sourceData: Data? -
Image dimensions.
Declaration
Swift
public var size: Size { get } -
Whether the image has an alpha channel.
Declaration
Swift
public var hasAlpha: Bool { get } -
One-time libvips initialization. Call once at app startup (e.g. in configure.swift).
Declaration
Swift
public static func initializeVips() -
Load an image from a data buffer. Auto-detects format and auto-rotates per EXIF.
Throws
ImageError.invalidImageif the data can’t be parsedDeclaration
Swift
public init(data: Data) throwsParameters
dataRaw image bytes (JPEG, PNG, GIF, WebP, TIFF, BMP, HEIC, AVIF, JXL)
-
Create an image from raw pixel data (e.g. for identicon/QR code generation).
Declaration
Swift
public init(width: Int, height: Int, pixels: [UInt8], bands: Int = 3) throwsParameters
widthImage width in pixels
heightImage height in pixels
pixelsRaw pixel data, must be exactly width * height * bands bytes
bandsNumber of color bands (3 for RGB, 4 for RGBA). Defaults to 3.
-
Internal init from an already-owned VipsImage pointer.
Declaration
Swift
private init(vipsImage: UnsafeMutablePointer<VipsImage>)
-
Resize to exact width and height.
Declaration
Swift
public func resizedTo(width: Int, height: Int) -> SwiftarrImage? -
Resize to exact width and height using nearest-neighbor interpolation. Produces crisp pixel-art scaling — use for QR codes and identicons.
Declaration
Swift
public func resizedToNearest(width: Int, height: Int) -> SwiftarrImage? -
Resize to a target width, preserving aspect ratio.
Declaration
Swift
public func resizedTo(width: Int) -> SwiftarrImage? -
Resize to a target height, preserving aspect ratio.
Declaration
Swift
public func resizedTo(height: Int) -> SwiftarrImage? -
Crop a rectangular region from the image.
Declaration
Swift
public func cropped(to rect: Rectangle) -> SwiftarrImage? -
Flatten alpha channel onto a solid background color. Returns nil if the image has no alpha channel.
Declaration
Swift
public func flattened(background: Color = .white) -> SwiftarrImage?
-
Export as JPEG data with the given quality (0-100).
Declaration
Swift
public func exportAsJPEG(quality: Int = 90) throws -> Data -
Export as PNG data.
Declaration
Swift
public func exportAsPNG() throws -> Data -
Create a resized animated thumbnail from raw GIF/WebP data, preserving all frames. Returns the resized animation as Data in the original format.
Declaration
Swift
public static func animatedThumbnail(from data: Data, height: Int, isGIF: Bool) throws -> Data -
Export as GIF data (static single frame).
Declaration
Swift
public func exportAsGIF() throws -> Data -
Export as WebP data with the given quality (0-100).
Declaration
Swift
public func exportAsWebP(quality: Int = 90) throws -> Data -
Declaration
Swift
deinit
View on GitHub