ImageX 1.1.7

ImageX 1.1.7

77 维护。



 
依赖关系
Harbeth>= 0
CacheX>= 0
 

ImageX 1.1.7

  • 作者
  • Condy

ImageX

Carthage compatible CocoaPods Compatible Platform

ImageX 是一个强大的库,它可以使控件快速播放动画图像并添加滤镜。核心是使用 CADisplayLink 不断刷新和更新动画图像帧。


英文 | 简体中文

功能

🧢目前,GIF 动画 最重要的功能可以总结如下:

  • 支持更多平台系统,macOS、iOS、tvOS、watchOS。
  • 支持以下格式的图像显示和解码
    webp, heic, gif。
  • 支持以下格式的静态图像显示和解码
    jpeg, png, tiff, heif, webp, heic, gif。
  • 支持直接从具有以下扩展名的 URL 设置图像或动画图像
    NSImageViewUIImageViewUIButtonNSButtonWKInterfaceImage
  • 如果使用了协议 AsAnimatable,则支持任何控件播放动画图像并设置滤镜。
  • 支持异步从网络下载和缓存图像或动画。
  • 支持使用相同 URL 网络共享,并且不会多次下载相同资源数据。
  • 支持网络资源数据的断点连续传输和下载。
  • 支持六种图像或动态图像内容模式
  • 支持磁盘和内存缓存的网络数据,数据由GZip压缩。
  • 支持对缓存数据的二次压缩,占用更少的磁盘空间。
  • 支持在空闲时间和大小限制下清理磁盘上的过期数据。
  • 支持设置不同类型的命名加密方法,例如md5、sha1、base58,以及用户自定义。

需求

iOS 目标 macOS 目标 Xcode 版本 Swift 版本
iOS 10.0+ macOS 10.13+ Xcode 10.0+ Swift 5.0+

支持项目

GitHub上购买我一杯咖啡或支持我。

yellow-button

使用方法

  • NSImageViewUIImageView显示网络图像或动画图像并添加过滤器。
// Set image from a url.
let url = URL(string: "https://example.com/image.png")!
imageView.mt.setImage(with: url)
  • 或设置其他参数播放动画图像或下载图像。
var options = ImageXOptions(moduleName: "Component Name")
options.placeholder = .image(R.image("AppIcon")!)
options.contentMode = .scaleAspectBottomRight
options.Animated.loop = .count(3)
options.Animated.bufferCount = 20
options.Cache.cacheOption = .disk
options.Cache.cacheCrypto = .md5
options.Cache.cacheDataZip = .gzip
options.Network.retry = .max3s
options.Network.timeoutInterval = 30
options.Animated.setPreparationBlock(block: { [weak self] _ in
    // do something..
})
options.Animated.setAnimatedBlock(block: { _ in
    // play is complete and then do something..
})
options.Network.setNetworkProgress(block: { _ in
    // download progress..
})
options.Network.setNetworkFailed(block: { _ in
    // download failed.
})

let links = [``GIF URL``, ``Image URL``, ``GIF Named``, ``Image Named``]
let named = links.randomElement() ?? ""

// Setup filters.
let filters: [C7FilterProtocol] = [
    C7SoulOut(soul: 0.75),
    C7Storyboard(ranks: 2),
]
imageView.mt.setImage(with: named, filters: filters, options: options)

😘😘其他方法

/// Display image or animated image and add the filters.
/// - Parameters:
///   - named: Picture or gif name.
///   - filters: Harbeth filters apply to image or gif frame.
///   - options: Represents creating options used in ImageX.
public func setImage(
    with named: String, 
    filters: [C7FilterProtocol], 
    options: ImageXOptions = ImageXOptions.default
)

/// Display image or animated image and add the filters.
/// - Parameters:
///   - data: Picture data.
///   - filters: Harbeth filters apply to image or gif frame.
///   - options: Represents creating options used in ImageX.
/// - Returns: A uniform type identifier UTI.
public func setImage(
    with data: Data?, 
    filters: [C7FilterProtocol], 
    options: ImageXOptions = ImageXOptions.default
) -> AssetType

/// Display network image or animated image and add the filters.
/// - Parameters:
///   - url: Link url.
///   - filters: Harbeth filters apply to image or gif frame.
///   - options: Represents gif playback creating options used in ImageX.
/// - Returns: Current network URLSessionDataTask.
public func setImage(
    with url: URL?, 
    filters: [C7FilterProtocol], 
    options: ImageXOptions = ImageXOptions.default
) -> Task?
  • 任何控件都可以播放本地动画图像数据。
let filters: [C7FilterProtocol] = [ ``Harbeth Filter`` ]
let data = R.gifData(``GIF Name``)
var options = ImageXOptions()
options.Animated.loop = .count(5)
options.placeholder = .color(.cyan)
animatedView.play(data: data, filters: filters, options: options)
  • 任何实现AsAnimatable协议的控件都可以支持动画图像播放。
class AnimatedView: UIView, AsAnimatable {
    ...
}

此处的ImageView已经实现了GIF动画支持,因此您可以直接使用它。✌️

内容模式

  • 主要用于图像填充内容改变大小。
示例 内容模式
original 原始
原始图像的尺寸。对此不做任何操作。

ImageXOptions(contentMode: .original)
scaleToFill scaleToFill
将内容缩放以适应大小,必要时更改内容的纵横比。

ImageXOptions(contentMode: .scaleToFill)
scaleAspectFit scaleAspectFit
内容缩放以匹配固定纵横比。剩余部分为透明。

ImageXOptions(contentMode: .scaleAspectFit)
scaleAspectFill scaleAspectFill
内容缩放以填充固定纵横比。部分内容可能被裁剪。

ImageXOptions(contentMode: .scaleAspectFill)
scaleAspectBottomRight scaleAspectBottomRight
内容缩放以填充固定纵横比。顶部或左侧的部分内容可能被裁剪。

ImageXOptions(contentMode: .scaleAspectBottomRight)
scaleAspectTopLeft scaleAspectTopLeft
内容缩放以填充固定纵横比。底部或右侧的部分内容可能被裁剪。

ImageXOptions(contentMode: .scaleAspectTopLeft)

ImageXOptions

  • 与动画播放、下载和缓存相关的其他参数。
  • 代表ImageX中使用的gif播放创建选项。
public struct ImageXOptions {
    
    public static var `default` = ImageXOptions()
    
    /// Additional parameters that need to be set to play animated images.
    public var Animated: ImageXOptions.Animated = ImageXOptions.Animated.init()
    
    /// Download additional parameters that need to be configured to download network resources.
    public var Network: ImageXOptions.Network = ImageXOptions.Network.init()
    
    /// Caching data from the web need to be configured parameters.
    public var Cache: ImageXOptions.Cache = ImageXOptions.Cache.init()
    
    /// 如果遇见设置`original`以外其他模式显示无效`铺满屏幕`的情况,
    /// 请将承载控件``view.contentMode = .scaleAspectFit``
    /// Content mode used for resizing the frames. Default is ``original``.
    public var contentMode: ImageX.ContentMode = .original
    
    /// Placeholder image. default gray picture.
    public var placeholder: ImageX.Placeholder = .none
    
    /// Confirm the size to facilitate follow-up processing, Default display control size.
    public var confirmSize: CGSize = .zero
    
    /// 做组件化操作时刻,解决本地GIF或本地图片所处于另外模块从而读不出数据问题。😤
    /// Do the component operation to solve the problem that the local animated image or Image cannot read the data in another module.
    public let moduleName: String
    
    /// Instantiation of configuration parameters.
    /// - Parameters:
    ///   - moduleName: Do the component operation to solve the problem that the local animated image or image cannot read the data in another module.
    public init(moduleName: String = "ImageX") {
        self.moduleName = moduleName
    }
}

AsAnimatable

  • 视图类需要遵循以启用动画图像支持的协议。
public protocol AsAnimatable: HasAnimatable {
    
    /// Total duration of one animation loop.
    var loopDuration: TimeInterval { get }
    
    /// The first frame that is not nil of animated images.
    var fristFrame: C7Image? { get }
    
    /// Returns the active frame if available.
    var activeFrame: C7Image? { get }
    
    /// Total frame count of the animated images.
    var frameCount: Int { get }
    
    /// Introspect whether the instance is animating.
    var isAnimating: Bool { get }
    
    /// Bitmap memory cost with bytes.
    var cost: Int { get }
    
    /// Stop animating and free up animated images data from memory.
    func prepareForReuse()
    
    /// Start animating animated image.
    func startAnimating()
    
    /// Stop animating animated image.
    func stopAnimating()
    
    /// Prepare for animation and start play animated image.
    /// - Parameters:
    ///   - data: gif data.
    ///   - filters: Harbeth filters apply to image or animated image frame.
    ///   - options: Represents animated image playback creating options used in ImageX.
    func play(data: Data?, filters: [C7FilterProtocol], options: ImageXOptions)
}

缓存的

  • 关于缓存模块的使用,请参阅Lemons获取更多信息。
  • 网络数据缓存类型。有两种模式:内存和磁盘存储。
  • 其中,磁盘存储使用GZip压缩数据,因此它将占用较少的空间。
  • 支持设置不同类型的命名加密方法,例如md5、sha1、base58以及用户自定义。
  • 考虑到不同级别的安全性,这里开启了数据源压缩和解压缩方法。库提供了GZip压缩或解压缩。当然,用户也可以自定义。

安装

CocoaPods

pod 'ImageX'

Swift 包管理器

dependencies: [
    .package(url: "https://github.com/yangKJ/ImageX.git", branch: "master"),
]

联系


许可

ImageX以MIT许可提供。有关更多信息,请参阅LICENSE文件。