Deprecated.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // Deprecated.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/28.
  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. #if canImport(AppKit)
  27. import AppKit
  28. #elseif canImport(UIKit)
  29. import UIKit
  30. #endif
  31. extension KingfisherClass where Base: Image {
  32. @available(*, deprecated, message:
  33. "Pass parameters with `ImageCreatingOptions`, use `image(with:options:)` instead.")
  34. public static func image(
  35. data: Data,
  36. scale: CGFloat,
  37. preloadAllAnimationData: Bool,
  38. onlyFirstFrame: Bool) -> Image?
  39. {
  40. let options = ImageCreatingOptions(
  41. scale: scale,
  42. duration: 0.0,
  43. preloadAll: preloadAllAnimationData,
  44. onlyFirstFrame: onlyFirstFrame)
  45. return KingfisherClass.image(data: data, options: options)
  46. }
  47. @available(*, deprecated, message:
  48. "Pass parameters with `ImageCreatingOptions`, use `animatedImage(with:options:)` instead.")
  49. public static func animated(
  50. with data: Data,
  51. scale: CGFloat = 1.0,
  52. duration: TimeInterval = 0.0,
  53. preloadAll: Bool,
  54. onlyFirstFrame: Bool = false) -> Image?
  55. {
  56. let options = ImageCreatingOptions(
  57. scale: scale, duration: duration, preloadAll: preloadAll, onlyFirstFrame: onlyFirstFrame)
  58. return animatedImage(data: data, options: options)
  59. }
  60. }
  61. @available(*, deprecated, message: "Use `Result<RetrieveImageResult>` based callback instead")
  62. public typealias CompletionHandler = ((_ image: Image?, _ error: NSError?, _ cacheType: CacheType, _ imageURL: URL?) -> Void)
  63. @available(*, deprecated, message: "Use `Result<ImageDownloadResult>` based callback instead")
  64. public typealias ImageDownloaderCompletionHandler = ((_ image: Image?, _ error: NSError?, _ url: URL?, _ originalData: Data?) -> Void)
  65. extension RetrieveImageTask {
  66. @available(*, deprecated, message: "RetrieveImageTask.empty will be removed soon. Use `nil` to represnt a no task.")
  67. public static let empty = RetrieveImageTask()
  68. }
  69. extension KingfisherManager {
  70. /// Get an image with resource.
  71. /// If `.empty` is used as `options`, Kingfisher will seek the image in memory and disk first.
  72. /// If not found, it will download the image at `resource.downloadURL` and cache it with `resource.cacheKey`.
  73. /// These default behaviors could be adjusted by passing different options. See `KingfisherOptions` for more.
  74. ///
  75. /// - Parameters:
  76. /// - resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  77. /// - options: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  78. /// - progressBlock: Called every time downloaded data changed. This could be used as a progress UI.
  79. /// - completionHandler: Called when the whole retrieving process finished.
  80. /// - Returns: A `RetrieveImageTask` task object. You can use this object to cancel the task.
  81. @available(*, deprecated, message: "Use `Result` based callback instead.")
  82. @discardableResult
  83. public func retrieveImage(with resource: Resource,
  84. options: KingfisherOptionsInfo?,
  85. progressBlock: DownloadProgressBlock?,
  86. completionHandler: CompletionHandler?) -> DownloadTask?
  87. {
  88. return retrieveImage(with: resource, options: options, progressBlock: progressBlock) {
  89. result in
  90. switch result {
  91. case .success(let value): completionHandler?(value.image, nil, value.cacheType, value.imageURL)
  92. case .failure(let error): completionHandler?(nil, error as NSError, .none, resource.downloadURL)
  93. }
  94. }
  95. }
  96. }
  97. extension ImageDownloader {
  98. @available(*, deprecated, message: "Use `Result` based callback instead.")
  99. @discardableResult
  100. open func downloadImage(with url: URL,
  101. retrieveImageTask: RetrieveImageTask? = nil,
  102. options: KingfisherOptionsInfo? = nil,
  103. progressBlock: ImageDownloaderProgressBlock? = nil,
  104. completionHandler: ImageDownloaderCompletionHandler?) -> DownloadTask?
  105. {
  106. return downloadImage(with: url, options: options, progressBlock: progressBlock) {
  107. result in
  108. switch result {
  109. case .success(let value): completionHandler?(value.image, nil, value.url, value.originalData)
  110. case .failure(let error): completionHandler?(nil, error as NSError, nil, nil)
  111. }
  112. }
  113. }
  114. }
  115. @available(*, deprecated, message: "RetrieveImageDownloadTask is removed. Use `SessionDataTask` to cancel a task.")
  116. public struct RetrieveImageDownloadTask {
  117. }
  118. @available(*, deprecated, message: "RetrieveImageTask is removed. Use `SessionDataTask` to cancel a task.")
  119. public final class RetrieveImageTask {
  120. }
  121. @available(*, deprecated, message: "Use `DownloadProgressBlock` instead.", renamed: "DownloadProgressBlock")
  122. public typealias ImageDownloaderProgressBlock = DownloadProgressBlock
  123. #if !os(watchOS)
  124. extension KingfisherClass where Base: ImageView {
  125. @available(*, deprecated, message: "Use `Result` based callback instead.")
  126. @discardableResult
  127. public func setImage(with resource: Resource?,
  128. placeholder: Placeholder? = nil,
  129. options: KingfisherOptionsInfo? = nil,
  130. progressBlock: DownloadProgressBlock? = nil,
  131. completionHandler: CompletionHandler?) -> DownloadTask?
  132. {
  133. return setImage(with: resource, placeholder: placeholder, options: options, progressBlock: progressBlock) {
  134. result in
  135. switch result {
  136. case .success(let value):
  137. completionHandler?(value.image, nil, value.cacheType, value.imageURL)
  138. case .failure(let error):
  139. completionHandler?(nil, error as NSError, .none, nil)
  140. }
  141. }
  142. }
  143. }
  144. extension KingfisherClass where Base: UIButton {
  145. @available(*, deprecated, message: "Use `Result` based callback instead.")
  146. @discardableResult
  147. public func setImage(
  148. with resource: Resource?,
  149. for state: UIControl.State,
  150. placeholder: UIImage? = nil,
  151. options: KingfisherOptionsInfo? = nil,
  152. progressBlock: DownloadProgressBlock? = nil,
  153. completionHandler: CompletionHandler?) -> DownloadTask?
  154. {
  155. return setImage(
  156. with: resource,
  157. for: state,
  158. placeholder: placeholder,
  159. options: options,
  160. progressBlock: progressBlock)
  161. {
  162. result in
  163. switch result {
  164. case .success(let value):
  165. completionHandler?(value.image, nil, value.cacheType, value.imageURL)
  166. case .failure(let error):
  167. completionHandler?(nil, error as NSError, .none, nil)
  168. }
  169. }
  170. }
  171. @available(*, deprecated, message: "Use `Result` based callback instead.")
  172. @discardableResult
  173. public func setBackgroundImage(
  174. with resource: Resource?,
  175. for state: UIControl.State,
  176. placeholder: UIImage? = nil,
  177. options: KingfisherOptionsInfo? = nil,
  178. progressBlock: DownloadProgressBlock? = nil,
  179. completionHandler: CompletionHandler?) -> DownloadTask?
  180. {
  181. return setBackgroundImage(
  182. with: resource,
  183. for: state,
  184. placeholder: placeholder,
  185. options: options,
  186. progressBlock: progressBlock)
  187. {
  188. result in
  189. switch result {
  190. case .success(let value):
  191. completionHandler?(value.image, nil, value.cacheType, value.imageURL)
  192. case .failure(let error):
  193. completionHandler?(nil, error as NSError, .none, nil)
  194. }
  195. }
  196. }
  197. }
  198. #endif
  199. extension ImageCache {
  200. /// The largest cache cost of memory cache. The total cost is pixel count of
  201. /// all cached images in memory.
  202. /// Default is unlimited. Memory cache will be purged automatically when a
  203. /// memory warning notification is received.
  204. @available(*, deprecated, message: "Use `memoryStorage.config.totalCostLimit` instead.",
  205. renamed: "memoryStorage.config.totalCostLimit")
  206. open var maxMemoryCost: Int {
  207. get { return memoryStorage.config.totalCostLimit }
  208. set { memoryStorage.config.totalCostLimit = newValue }
  209. }
  210. /// The default DiskCachePathClosure
  211. @available(*, deprecated, message: "Not needed anymore.")
  212. public final class func defaultDiskCachePathClosure(path: String?, cacheName: String) -> String {
  213. let dstPath = path ?? NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
  214. return (dstPath as NSString).appendingPathComponent(cacheName)
  215. }
  216. /// The default file extension appended to cached files.
  217. @available(*, deprecated, message: "Use `diskStorage.config.pathExtension` instead.",
  218. renamed: "diskStorage.config.pathExtension")
  219. open var pathExtension: String? {
  220. get { return diskStorage.config.pathExtension }
  221. set { diskStorage.config.pathExtension = newValue }
  222. }
  223. ///The disk cache location.
  224. @available(*, deprecated, message: "Use `diskStorage.directoryURL.absoluteString` instead.",
  225. renamed: "diskStorage.directoryURL.absoluteString")
  226. public var diskCachePath: String {
  227. return diskStorage.directoryURL.absoluteString
  228. }
  229. /// The largest disk size can be taken for the cache. It is the total
  230. /// allocated size of cached files in bytes.
  231. /// Default is no limit.
  232. @available(*, deprecated, message: "Use `diskStorage.config.sizeLimit` instead.",
  233. renamed: "diskStorage.config.sizeLimit")
  234. open var maxDiskCacheSize: UInt {
  235. get { return UInt(diskStorage.config.sizeLimit) }
  236. set { diskStorage.config.sizeLimit = Int(newValue) }
  237. }
  238. @available(*, deprecated, message: "Use `diskStorage.cacheFileURL(forKey:).path` instead.",
  239. renamed: "diskStorage.cacheFileURL(forKey:)")
  240. open func cachePath(forComputedKey key: String) -> String {
  241. return diskStorage.cacheFileURL(forKey: key).path
  242. }
  243. /**
  244. Get an image for a key from disk.
  245. - parameter key: Key for the image.
  246. - parameter options: Options of retrieving image. If you need to retrieve an image which was
  247. stored with a specified `ImageProcessor`, pass the processor in the option too.
  248. - returns: The image object if it is cached, or `nil` if there is no such key in the cache.
  249. */
  250. @available(*, deprecated, message: "Use `Result` based `retrieveImageInDiskCache(forKey:options:callbackQueue:completionHandler:)` instead.",
  251. renamed: "retrieveImageInDiskCache(forKey:options:callbackQueue:completionHandler:)")
  252. open func retrieveImageInDiskCache(forKey key: String, options: KingfisherOptionsInfo? = nil) -> Image? {
  253. let options = options ?? .empty
  254. let computedKey = key.computedKey(with: options.processor.identifier)
  255. do {
  256. if let data = try diskStorage.value(forKey: computedKey) {
  257. return options.cacheSerializer.image(with: data, options: options)
  258. }
  259. } catch {}
  260. return nil
  261. }
  262. @available(*, deprecated, message: "Use `Result` based `retrieveImage(forKey:options:callbackQueue:completionHandler:)` instead.",
  263. renamed: "retrieveImage(forKey:options:callbackQueue:completionHandler:)")
  264. open func retrieveImage(forKey key: String,
  265. options: KingfisherOptionsInfo?,
  266. completionHandler: ((Image?, CacheType) -> Void)?)
  267. {
  268. retrieveImage(
  269. forKey: key,
  270. options: options,
  271. callbackQueue: .dispatch((options ?? .empty).callbackDispatchQueue))
  272. {
  273. result in
  274. completionHandler?(result.value?.image, result.value?.cacheType ?? .none)
  275. }
  276. }
  277. /// The longest time duration in second of the cache being stored in disk.
  278. /// Default is 1 week (60 * 60 * 24 * 7 seconds).
  279. /// Setting this to a negative value will make the disk cache never expiring.
  280. @available(*, deprecated, message: "Deprecated. Use `diskStorage.config.expiration` instead")
  281. open var maxCachePeriodInSecond: TimeInterval {
  282. get { return diskStorage.config.expiration.timeInterval }
  283. set { diskStorage.config.expiration = .seconds(newValue) }
  284. }
  285. }