Skip to content

Commit

Permalink
Add compression quality version for CacheSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 14, 2019
1 parent 2f9d171 commit 0b0fda8
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Sources/Cache/FormatIndicatedCacheSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// THE SOFTWARE.

import Foundation
import CoreGraphics

/// `FormatIndicatedCacheSerializer` lets you indicate an image format for serialized caches.
///
Expand Down Expand Up @@ -55,18 +56,31 @@ public struct FormatIndicatedCacheSerializer: CacheSerializer {

/// A `FormatIndicatedCacheSerializer` which converts image from and to PNG format. If the image cannot be
/// represented by PNG format, it will fallback to its real format which is determined by `original` data.
public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG, jpegCompressionQuality: nil)

/// A `FormatIndicatedCacheSerializer` which converts image from and to JPEG format. If the image cannot be
/// represented by JPEG format, it will fallback to its real format which is determined by `original` data.
public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
/// The compression quality is 1.0 when using this serializer. If you need to set a customized compression quality,
/// use `jpeg(compressionQuality:)`.
public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG, jpegCompressionQuality: 1.0)

/// A `FormatIndicatedCacheSerializer` which converts image from and to JPEG format with a settable compression
/// quality. If the image cannot be represented by JPEG format, it will fallback to its real format which is
/// determined by `original` data.
/// - Parameter compressionQuality: The compression quality when converting image to JPEG data.
public static func jpeg(compressionQuality: CGFloat) -> FormatIndicatedCacheSerializer {
return FormatIndicatedCacheSerializer(imageFormat: .JPEG, jpegCompressionQuality: compressionQuality)
}

/// A `FormatIndicatedCacheSerializer` which converts image from and to GIF format. If the image cannot be
/// represented by GIF format, it will fallback to its real format which is determined by `original` data.
public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)
public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF, jpegCompressionQuality: nil)

/// The indicated image format.
private let imageFormat: ImageFormat

/// The compression quality used for loss image format (like JPEG).
private let jpegCompressionQuality: CGFloat?

/// Creates data which represents the given `image` under a format.
public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
Expand All @@ -75,7 +89,7 @@ public struct FormatIndicatedCacheSerializer: CacheSerializer {
return autoreleasepool { () -> Data? in
switch imageFormat {
case .PNG: return image.kf.pngRepresentation()
case .JPEG: return image.kf.jpegRepresentation(compressionQuality: 1.0)
case .JPEG: return image.kf.jpegRepresentation(compressionQuality: jpegCompressionQuality ?? 1.0)
case .GIF: return image.kf.gifRepresentation()
case .unknown: return nil
}
Expand Down

0 comments on commit 0b0fda8

Please sign in to comment.