added iOS source code
[wl-app.git] / iOS / Pods / Kingfisher / Sources / CacheSerializer.swift
1 //
2 //  CacheSerializer.swift
3 //  Kingfisher
4 //
5 //  Created by Wei Wang on 2016/09/02.
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 /// An `CacheSerializer` would be used to convert some data to an image object for 
30 /// retrieving from disk cache and vice versa for storing to disk cache.
31 public protocol CacheSerializer {
32     
33     /// Get the serialized data from a provided image
34     /// and optional original data for caching to disk.
35     ///
36     ///
37     /// - parameter image:    The image needed to be serialized.
38     /// - parameter original: The original data which is just downloaded. 
39     ///                       If the image is retrieved from cache instead of
40     ///                       downloaded, it will be `nil`.
41     ///
42     /// - returns: A data which will be stored to cache, or `nil` when no valid
43     ///            data could be serialized.
44     func data(with image: Image, original: Data?) -> Data?
45     
46     /// Get an image deserialized from provided data.
47     ///
48     /// - parameter data:    The data from which an image should be deserialized.
49     /// - parameter options: Options for deserialization.
50     ///
51     /// - returns: An image deserialized or `nil` when no valid image 
52     ///            could be deserialized.
53     func image(with data: Data, options: KingfisherOptionsInfo?) -> Image?
54 }
55
56
57 /// `DefaultCacheSerializer` is a basic `CacheSerializer` used in default cache of
58 /// Kingfisher. It could serialize and deserialize PNG, JEPG and GIF images. For 
59 /// image other than these formats, a normalized `pngRepresentation` will be used.
60 public struct DefaultCacheSerializer: CacheSerializer {
61     
62     public static let `default` = DefaultCacheSerializer()
63     private init() {}
64     
65     public func data(with image: Image, original: Data?) -> Data? {
66         let imageFormat = original?.kf.imageFormat ?? .unknown
67
68         let data: Data?
69         switch imageFormat {
70         case .PNG: data = image.kf.pngRepresentation()
71         case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
72         case .GIF: data = image.kf.gifRepresentation()
73         case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
74         }
75
76         return data
77     }
78     
79     public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
80         let options = options ?? KingfisherEmptyOptionsInfo
81         return Kingfisher<Image>.image(
82             data: data,
83             scale: options.scaleFactor,
84             preloadAllAnimationData: options.preloadAllAnimationData,
85             onlyFirstFrame: options.onlyLoadFirstFrame)
86     }
87 }