From 0b0fda8305ce1278863665d163d0c0428e157ac0 Mon Sep 17 00:00:00 2001 From: onevcat Date: Sat, 14 Dec 2019 23:26:37 +0900 Subject: [PATCH] Add compression quality version for CacheSerializer --- .../FormatIndicatedCacheSerializer.swift | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Sources/Cache/FormatIndicatedCacheSerializer.swift b/Sources/Cache/FormatIndicatedCacheSerializer.swift index f408f5e41..cdfb7c349 100644 --- a/Sources/Cache/FormatIndicatedCacheSerializer.swift +++ b/Sources/Cache/FormatIndicatedCacheSerializer.swift @@ -25,6 +25,7 @@ // THE SOFTWARE. import Foundation +import CoreGraphics /// `FormatIndicatedCacheSerializer` lets you indicate an image format for serialized caches. /// @@ -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? { @@ -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 }