ImageIO.Swift
ImageIO.Swift 使在 Apple 平台上处理图像变得容易。它整合了SDWebImage、FLAnimatedImage 和 Concorde 功能!
- 异步下载图像。
- 动态 GIF、PNG 和 (iOS 13 中) HEIC!
- 增量加载交错和非交错 JPEG。
- 直接从文件生成缩略图(特别是对于 HEIC 图像很有用,因为它们经常嵌入预渲染的缩略图)。
- 检查图像细节和 EXIF 元数据。
使用方法
UIKit (ImageIOUIKit)
ImageSourceView
处理图像的加载和显示。
// display an image downloaded from a URL
let view = ImageSourceView()
view.isAnimationEnabled = true
view.load(url)
如果同一屏幕上的多个位置都显示了相同的图像 URL(例如,新闻源布局中单个用户的个人资料图片),则它将只下载一次并将其加载到内存中。它还将智能缓存,以便后续请求将重用内存和下载的数据。
您还可以直接设置图像源
let view = ImageSourceView()
view.isAnimationEnabled = true
view.imageSource = imageSource
您可以访问视图的 imageSource
(无论您是否直接设置或从 URL 加载它)并订阅其 didUpdateData
通知以跟踪其下载。要获取不同动画帧的更新,您可以选择继承 ImageSourceView
或使用 KVO 与 displayedImage
。
UIKit模块还扩展了 ImageSource
,以便访问正确方向的 UIImage
(这是一个 CGImage
不考虑的特性)。
实验性:SwiftUI (ImageIOSwiftUI)
支持iOS、macOS、watchOS和tvOS!
URLImageSourceView
的工作方式类似于html中的 img
标签。
// display an image downloaded from a URL
URLImageSourceView(
url: url,
isAnimationEnabled: true,
label: Text("Alt text")
)
如果同一屏幕上多处显示了相同的图片URL(例如,新闻源布局中单个用户的个人资料图片),则只需下载和加载到内存中一次。它还将智能缓存,以便后续请求将重用内存和下载的数据。
如果您想单独加载图像源,可以使用 ImageSourceView
// display an image source, animating if possible
ImageSourceView(
imageSource: imageSource,
isAnimationEnabled: true,
label: Text("Alt text")
)
最后,如果您想自定义图像渲染方式,可以为 URLImageSourceView
或 ImageSourceView
提供自己的内容
// places an animation progress bar at the bottom of the image
URLImageSourceView(
url: url,
isAnimationEnabled: true,
label: Text("Alt text")
) { imageSource, animationFrame, label in
StaticImageSourceView(imageSource: imageSource, animationFrame: animationFrame, label: label)
.aspectRatio(contentMode: .fit)
.overlay(
Rectangle()
.fill(Color.blue.opacity(0.5))
.frame(height: 10)
.relativeWidth(Length(imageSource.progress(atFrame: animationFrame))),
alignment: .bottomLeading
)
}
每当图像数据更新或动画帧改变时,都会调用内容回调。默认情况下,使用 StaticImageSourceView
显示图像帧,您可以用它作为自定义的基础。
如果您想在图像加载时提供一个占位符,推荐的做法是使用 ZStack
// load an image, clipped by a circle with a gray background and placeholder image
ZStack {
Image(systemName: "person.fill")
.foregroundColor(Color(white: 0.4))
URLImageSourceView(url: user.picture.medium)
.frame(width: 36, height: 36)
}
.frame(width: 36, height: 36)
.background(Color(white: 0.8))
.clipShape(Circle())
这样,随着图像的加载,将显示增量加载的图像。
免责声明:我曾希望SwiftUI在夏季变得更加稳定,但它仍然有很大漏洞和不完善。虽然我了解这个项目在SwiftUI方面的支持没有问题,但这不是我正在使用的任何项目中的SwiftUI,因此我无法为这个项目的稳定性作证。
ImageSource
您可以将 CG/NS/UIImage
视为一个像素帧。在它下面是 ImageSource
,它提供了对几乎所有图像文件提供的内容的访问,包括元数据和多个表示形式。例如,动态图像有多帧图像以及时间元数据。
您可以访问诸如 count
(动画图像中的帧数)或 typeIdentifier
来获取文件类型。但它的主要用途是生成图像
imageSource.cgImage(at: 3) // frame 3
// with UIKit integration:
imageSource.image(at: 3)
您可以选择图像生成的选项。
// decode the image data immediately instead of lazily when it gets drawn for the first time
// this is especially useful if you're loading images in a background thread before passing them to the main thread
var options = ImageSource.ImageOptions()
options.shouldDecodeImmediately = false
imageSource.cgImage(options: options)
创建缩略图类似
imageSource.cgThumbnailImage(size: thumbnailSize, mode: .fill)
请注意,图像源不支持裁剪,因此它总是返回与原始图像相同的宽高比的图像。如果图像包含嵌入式缩略图,这可能比普通缩略图渲染快得多。
因为图像源可以引用磁盘上的文件,所以您可以在不将整个文件加载到内存中的情况下加载图像的元数据。这对获取图像大小非常有用。
ImageSource(url: fileURL).properties(at: 0)?.imageSize
请注意,如果图像源正在增量加载或引用无效文件,大小将为 nil。
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
安装
Swift 包管理器
在 Xcode 11 中,您可以通过项目的 Github URL 在项目设置中将 ImageIOSwift 添加到您的项目中作为包。然后,您可以链接到您需要的包(ImageIOSwift、ImageIOSwiftUI 或 ImageIOUIKit)。
在 macOS 上,您可以通过在您的Package.swift中添加以下内容使用命令行使用它:
dependencies: [
.package(url: "https://github.com/davbeck/ImageIOSwift.git", from: "0.5.0"),
],
注意,由于 ImageIO 在 Linux(或任何非 Apple 平台)上不可用,因此此包在那里不能使用。
CocoaPods
将以下行添加到您的 Podfile 中
pod 'ImageIOSwift'
# one of both of these
pod 'ImageIOSwiftUI'
pod 'ImageIOUIKit'
许可
ImageIOSwift 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。