Longinus
Longinus是一个纯Swift的高性能异步Web图片加载、缓存和编辑框架。
它借鉴了Objective-C的Web图片加载框架YYWebImage和BBWebImage,为Swift带来了许多高性能特性。它可能成为您的更好的选择。
Longinus的目的是成为Swift上性能最高的Web图片加载框架。
特性
- 异步图片下载和缓存。
- 预加载数据并将图片缓存到磁盘以供后续展示。
- 支持动画GIF(动态缓冲,内存使用量更低)。
- 支持基线/渐进/交错模式图片解码。
- 对UIImageView、UIButton、MKAnnotationView(尚未加入)和CALayer的视图扩展,可以直接从URL设置图片。
- 设置图片时内置的过渡动画。(或者您可以设置自定义的图片显示过渡动画)
- 支持在下载后进行图片变换:模糊、圆角、缩放、颜色调和、裁剪、旋转等。
- 高性能的内存和磁盘图片缓存。使用LRU算法进行管理。对于磁盘缓存,它使用文件系统和sqlite以提高性能。
- 使用FIFO队列处理图片下载操作。
- 无滞后感的平滑滑动。高性能的图片缓存和解码以避免主线程阻塞。
- 支持SwiftUI。
使用方法
最简单的用例是将图片设置到一个使用UIImageView扩展的imageView中
let url = URL(string: "http://github.com/logo.png")
imageView.lg.setImage(with: url)
加载动画GIF图像
let url = URL(string: "https://ww4.sinaimg.cn/bmiddle/eaeb7349jw1ewbhiu69i2g20b4069e86.gif")
imageView.lg.setImage(with: url)
渐进加载图像
let url = URL(string: "http://github.com/logo.png")
imageView.lg.setImage(with: url, options: [.progressiveBlur, .imageWithFadeAnimation])
加载和变换图像
let url = URL(string: "https://ww4.sinaimg.cn/bmiddle/eaeb7349jw1ewbhiu69i2g20b4069e86.gif")
let transformer = ImageTransformer.imageTransformerCommon(with: imageView.frame.size, borderWidth: 2.0, borderColor: .white)
imageView.lg.setImage(with: url, options: [.progressiveBlur, .imageWithFadeAnimation], transformer: transformer)
在SwiftUI中使用
import SwiftUI
// 1. If you are using SPM or Carthage, the SwiftUI support is defined in a new module.
import LonginusSwiftUI
// 2. If you are using CocoaPods, in which the SwiftUI support is defined in the Longinus module.
// Here we choose to just import the `LGImage` type instead of the whole module,
// to prevent the conflicting between `Longinus.View` and `SwiftUI.View`
import struct Longinus.LGImage
var body: some View {
LGImage(source: URL(string: "https://github.com/KittenYang/Template-Image-Set/blob/master/Landscape/landscape-\(index).jpg?raw=true"), placeholder: {
Image(systemName: "arrow.2.circlepath")
.font(.largeTitle) })
.onProgress(progress: { (data, expectedSize, _) in
print("Downloaded: \(data?.count ?? 0)/\(expectedSize)")
})
.onCompletion(completion: { (image, data, error, cacheType) in
if let error = error {
print(error)
}
if let _ = image {
print("Success!")
}
})
.resizable()
.cancelOnDisappear(true)
.aspectRatio(contentMode: .fill)
.frame(width: 300, height: 300)
.cornerRadius(20)
.shadow(radius: 5)
}
需求
- ios 10.0+
- Swift 5.0+
- SwiftUI 13.0+
安装
CocoaPods
Longinus可通过CocoaPods获取。要安装,请简单地将以下行添加到您的Podfile中
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target 'MyApp' do
# your other pod
# ...
pod 'Longinus'
# SwiftUI support is provided in a sub-spec.
# So instead of specifying pod 'Longinus',
# you need:
# pod 'Longinus/SwiftUI'
end
然后,运行以下命令
$ pod install
安装CocoaPods中的任何内容后,您应该打开.{Project}.xcworkspace而不是.{Project}.xcodeproj。
有关如何使用CocoaPods的更多信息,我建议查看这个教程。
Carthage
Carthage是用于Cocoa应用的去中心化依赖项管理器。要安装carthage工具,您可以使用Homebrew。
要使用Carthage将Longinus集成到您的Xcode项目中,请在其Cartfile中指定它
github "KittenYang/Longinus" ~> 1.0
然后,运行以下命令以构建Longinus框架
$ carthage update
Swift包管理器
从Xcode 11开始,您可以使用Swift包管理器将Longinus添加到您的项目中。
选择文件 > Swift包 > 添加包依赖项。在“选择包存储库”对话框中输入https://github.com/KittenYang/Longinus.git
。
基准
我从一些方面测试了iOS平台上一些流行的网络图像加载框架。
- 图像加载速度。
- 内存和磁盘读写/删除速度。
- 滚动4000张图像的用户界面fps。
以下是测试结果。(越低越好)
注意:测试设备是iPhone 11,运行iOS 13.3。
您可以git clone此存储库并运行Benchmark.xcworkspace
来自行进行测试。
许可
Longinus遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。