ImageIOUIKit 1.1.1

ImageIOUIKit 1.1.1

David Beck 维护。



ImageIO.Swift

CI Status Version License Platform

ImageIO.Swift 让在 Apple 平台上处理图片变得简单。它整合了SDWebImageFLAnimatedImageConcorde

  • 异步下载图片。
  • 动画面频、APNG 和 iOS 13 中的 HEIC 图片!(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

无论您是直接设置它还是从 URL 加载它,您都可以访问 imageSource 视图,并订阅它的 `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")
)

最后,如果您想自定义图片渲染方式,可以向URLImageSourceViewImageSourceView提供自己的内容

// 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。

示例

要运行示例项目,请克隆仓库,然后首先从示例目录中运行 pod install

安装

Swift Package Manager

在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(或任何非苹果平台)上不可用,因此此包不能在那里使用。

CocoaPods

请将以下行添加到您的Podfile中:

pod 'ImageIOSwift'
# one of both of these
pod 'ImageIOSwiftUI'
pod 'ImageIOUIKit'

许可证

ImageIOSwift在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。

示例图片源