Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compression quality version for CacheSerializer #1372

Merged
merged 1 commit into from
Dec 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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