2 // RequestModifier.swift
5 // Created by Junyu Kuang on 5/28/17.
7 // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 /// `FormatIndicatedCacheSerializer` let you indicate an image format for serialized caches.
31 /// It could serialize and deserialize PNG, JEPG and GIF images. For
32 /// image other than these formats, a normalized `pngRepresentation` will be used.
36 /// private let profileImageSize = CGSize(width: 44, height: 44)
38 /// private let imageProcessor = RoundCornerImageProcessor(
39 /// cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)
41 /// private let optionsInfo: KingfisherOptionsInfo = [
42 /// .cacheSerializer(FormatIndicatedCacheSerializer.png),
43 /// .backgroundDecode, .processor(imageProcessor), .scaleFactor(UIScreen.main.scale)]
45 /// extension UIImageView {
46 /// func setProfileImage(with url: URL) {
47 /// // Image will always cached as PNG format to preserve alpha channel for round rect.
48 /// _ = kf.setImage(with: url, options: optionsInfo)
52 public struct FormatIndicatedCacheSerializer: CacheSerializer {
54 public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
55 public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
56 public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)
58 /// The indicated image format.
59 private let imageFormat: ImageFormat
61 public func data(with image: Image, original: Data?) -> Data? {
63 func imageData(withFormat imageFormat: ImageFormat) -> Data? {
65 case .PNG: return image.kf.pngRepresentation()
66 case .JPEG: return image.kf.jpegRepresentation(compressionQuality: 1.0)
67 case .GIF: return image.kf.gifRepresentation()
68 case .unknown: return nil
72 // generate data with indicated image format
73 if let data = imageData(withFormat: imageFormat) {
77 let originalFormat = original?.kf.imageFormat ?? .unknown
79 // generate data with original image's format
80 if originalFormat != imageFormat, let data = imageData(withFormat: originalFormat) {
84 return original ?? image.kf.normalized.kf.pngRepresentation()
87 /// Same implementation as `DefaultCacheSerializer`.
88 public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
89 let options = options ?? KingfisherEmptyOptionsInfo
90 return Kingfisher<Image>.image(
92 scale: options.scaleFactor,
93 preloadAllAnimationData: options.preloadAllAnimationData,
94 onlyFirstFrame: options.onlyLoadFirstFrame)