SwiftImageReadWrite

main

A basic microframework of routines for doing basic importing/exporting of `CGImage` and `NSImage`/`UIImage` type images.
dagronf/SwiftImageReadWrite

SwiftImageReadWrite

A basic Swift microframework of routines for doing basic importing/exporting of CGImage and NSImage/UIImage type images.

Swift Package Manager

Why?

Sometimes all you need is to be able load and save basic image data in different formats. This framework provides a basic wrapper around the common use cases.

Apple's built in image types are incredibly capable but can be quite verbose to do basic import and export to different image file types. It's also somewhat tricky to get basic export correct, so this framework abstracts that away with a type-safe, format safe way.

There are also differences between macOS and the rest of the Apple ecosystem when it comes to converting platform images and this library abstracts those differences away. The same API will work on macOS/iOS/watchOS and tvOS.

It also provides Codable wrapper implementations for CGImage/NSImage/UIImage (CGImageCodable, PlatformImageCodable) to allow easy embedding of images in your Codable objects.

Online documentation

Supported types

  • PNG
  • JPEG
  • TIFF
  • GIF
  • HEIC
  • PDF
  • SVG

Loading a CGImage

Two static methods have been added to CGImage

// Load a CGImage from raw data
let image = try CGImage.load(data: <someData>)
// Load a CGImage from a local file on disk
let image = try CGImage.load(fileURL: <someURL>)
// Load a CGImage from a named image asset
let image = try CGImage.named("my_image_name")

Generating different image representations

All the encoding/conversion calls are wrapped inside representation on an image object. This was done to avoid clashing with the platform image calls of similar names.

For example :-

let image = CGImage/UIImage/NSImage

// Generate a PNG representation
let pngData = try image.representation.png(scale: 2)

// Generate a JPG representation at 3x
let jpegData = try image.representation.jpeg(scale: 3, compression: 0.65, excludeGPSData: true))

// Generate a PDF representation
let pdfData = try image.representation.pdf(size: CGSize(width: 300, height: 300))

Basic examples

CGImage

// Load a CGImage from raw data
let cgImage = try CGImage.load(data: data)

// Export the image as JPG data
let jpegData = try cgImage.representation.jpeg(scale: 3, compression: 0.65, excludeGPSData: true))

// Export the image as PNG data
let pngData = try cgImage.representation.png(scale: 2)

// Generate an NSImage
let nsImage = cgImage.nsImage(scale: 2)

// Generate an UIImage
let nsImage = cgImage.uiImage()

NSImage/UIImage

Generating an NSImage or UIImage representation for a CGImage

// Load a CGImage from raw data
let cgImage = CGImage.load(data: data)

// Convert to an NSImage
let nsImage = cgImage.nsImage(scale: 2)

// Convert to a UIImage
let uiImage = cgImage.uiImage(scale: 3)

// Generate a PDF representation of the pdf
let pdf = try cgImage.representation.pdf()

SwiftUI

// Load a CGImage from raw data
let cgImage = CGImage.load(data: data)

// Create a SwiftUI image with this image
let swiftUIImage = cgImage.representation.swiftUI(scale: 2, label: Text("My Image"))

Limitations

  • NSImage supports multiple image representations within a single image. These routines only deal with a single representation, so an image with multiple stored representations will only ever deal with the 'best' representation.

License

MIT License

Copyright (c) 2024 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Description

  • Swift Tools 5.4.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Apr 22 2024 16:09:22 GMT-0900 (Hawaii-Aleutian Daylight Time)