added iOS source code
[wl-app.git] / iOS / Pods / Kingfisher / Sources / FormatIndicatedCacheSerializer.swift
1 //
2 //  RequestModifier.swift
3 //  Kingfisher
4 //
5 //  Created by Junyu Kuang on 5/28/17.
6 //
7 //  Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
8 //
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:
15 //
16 //  The above copyright notice and this permission notice shall be included in
17 //  all copies or substantial portions of the Software.
18 //
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
25 //  THE SOFTWARE.
26
27 import Foundation
28
29 /// `FormatIndicatedCacheSerializer` let you indicate an image format for serialized caches.
30 ///
31 /// It could serialize and deserialize PNG, JEPG and GIF images. For
32 /// image other than these formats, a normalized `pngRepresentation` will be used.
33 ///
34 /// Example:
35 /// ````
36 /// private let profileImageSize = CGSize(width: 44, height: 44)
37 ///
38 /// private let imageProcessor = RoundCornerImageProcessor(
39 ///     cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)
40 ///
41 /// private let optionsInfo: KingfisherOptionsInfo = [
42 ///     .cacheSerializer(FormatIndicatedCacheSerializer.png), 
43 ///     .backgroundDecode, .processor(imageProcessor), .scaleFactor(UIScreen.main.scale)]
44 ///
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)
49 ///    }
50 ///}
51 /// ````
52 public struct FormatIndicatedCacheSerializer: CacheSerializer {
53     
54     public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
55     public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
56     public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)
57     
58     /// The indicated image format.
59     private let imageFormat: ImageFormat
60     
61     public func data(with image: Image, original: Data?) -> Data? {
62         
63         func imageData(withFormat imageFormat: ImageFormat) -> Data? {
64             switch imageFormat {
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
69             }
70         }
71         
72         // generate data with indicated image format
73         if let data = imageData(withFormat: imageFormat) {
74             return data
75         }
76         
77         let originalFormat = original?.kf.imageFormat ?? .unknown
78         
79         // generate data with original image's format
80         if originalFormat != imageFormat, let data = imageData(withFormat: originalFormat) {
81             return data
82         }
83         
84         return original ?? image.kf.normalized.kf.pngRepresentation()
85     }
86     
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(
91             data: data,
92             scale: options.scaleFactor,
93             preloadAllAnimationData: options.preloadAllAnimationData,
94             onlyFirstFrame: options.onlyLoadFirstFrame)
95     }
96 }