一个基础的 Swift 微框架,提供用于进行 CGImage
和 NSImage
/UIImage
类型图像的基本导入/导出的例程。
有时您只需要能够以不同的格式加载和保存基本图像数据。这个框架为常见的用例提供了一个基本的封装。
Apple 内置的图像类型功能非常强大,但对于不同图像文件类型的基本导入和导出而言,代码可能会非常冗长。而且,要正确地进行基本导出也有些棘手,因此这个框架使用类型安全、格式安全的方式将其抽象化。
在转换平台图像时,macOS 和 Apple 生态系统的其他部分之间也存在差异,而此库可以消除这些差异。相同的 API 将适用于 macOS/iOS/watchOS 和 tvOS。
它还为 CGImage/NSImage/UIImage (CGImageCodable
, PlatformImageCodable
) 提供了 Codable
封装器实现,以便可以轻松地将图像嵌入到您的 Codable
对象中。
已向 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")
所有编码/转换调用都封装在图像对象的 representation
中。这样做是为了避免与平台图像调用中类似名称的调用发生冲突。
例如:-
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))
// 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()
// 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()
// This loads an `NSImage` or `UIImage` depending on the current platform
let platformImage = PlatformImage.load(data: data)
// This loads an `NSImage` or `UIImage` depending on the current platform
let platformImage = PlatformImage.load(fileURL: <some url>)
// 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"))
此库提供到 CMYK、灰度和 sRGB 的基本颜色空间转换。
let cmykImage = try image.convertColorspace.genericCMYK()
let grayImage = try image.convertColorspace.deviceGray()
let sRGBImage = try image.convertColorspace.sRGB()
NSImage
支持单个图像内的多个图像表示形式。这些例程只处理单个表示形式,因此具有多个存储的表示形式的图像将只处理“最佳”表示形式。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.