ImageIO.Swift
ImageIO.Swift 使得在 Apple 平台上处理图片变得轻松。它是 SDWebImage,FLAnimatedImage 和 Concorde 的集合!
- 异步下载图片。
- 动画 GIF,PNG 和(在 iOS 13 中)HEICs!
- 分段加载交错和 渐进式 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会在夏季变得更加稳定,但它的bug仍然很多,功能也尚未完善。虽然我不了解这个项目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 文件。