PINRemoteImageManager.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //
  2. // PINRemoteImageManager.h
  3. // Pods
  4. //
  5. // Created by Garrett Moon on 8/17/14.
  6. //
  7. //
  8. #import <Foundation/Foundation.h>
  9. #if PIN_TARGET_IOS
  10. #import <UIKit/UIKit.h>
  11. #elif PIN_TARGET_MAC
  12. #import <Cocoa/Cocoa.h>
  13. #endif
  14. #import "PINRemoteImageMacros.h"
  15. #import "PINRemoteImageManagerResult.h"
  16. @protocol PINRemoteImageManagerAlternateRepresentationProvider;
  17. @protocol PINRemoteImageCaching;
  18. @class PINRemoteImageManagerResult;
  19. extern NSString * __nonnull const PINRemoteImageManagerErrorDomain;
  20. /**
  21. Error codes returned by PINRemoteImage
  22. */
  23. typedef NS_ENUM(NSUInteger, PINRemoteImageManagerError) {
  24. /** The image failed to decode */
  25. PINRemoteImageManagerErrorFailedToDecodeImage = 1,
  26. /** The image could not be downloaded and therefore could not be processed */
  27. PINRemoteImageManagerErrorFailedToFetchImageForProcessing = 2,
  28. /** The image returned by the processor block was nil */
  29. PINRemoteImageManagerErrorFailedToProcessImage = 3,
  30. /** The image in the cache was invalid */
  31. PINRemoteImageManagerErrorInvalidItemInCache = 4,
  32. /** The image at the URL was empty */
  33. PINRemoteImageManagerErrorImageEmpty = 5,
  34. };
  35. /**
  36. Options with which to download and process images
  37. */
  38. typedef NS_OPTIONS(NSUInteger, PINRemoteImageManagerDownloadOptions) {
  39. /** Download and process with default options (no other options set) */
  40. PINRemoteImageManagerDownloadOptionsNone = 0,
  41. /** Set to disallow any alternate representations such as FLAnimatedImage */
  42. PINRemoteImageManagerDisallowAlternateRepresentations = 1,
  43. /** Skip decoding the image before returning. This means smaller images returned, but images will be decoded on the main thread when set on an image view */
  44. PINRemoteImageManagerDownloadOptionsSkipDecode = 1 << 1,
  45. /** Skip the early check of the memory cache */
  46. PINRemoteImageManagerDownloadOptionsSkipEarlyCheck = 1 << 2,
  47. /** Save processed images as JPEGs in the cache. The default is PNG to support transparency */
  48. PINRemoteImageManagerSaveProcessedImageAsJPEG = 1 << 3,
  49. /** Ignore cache and force download */
  50. PINRemoteImageManagerDownloadOptionsIgnoreCache = 1 << 4,
  51. };
  52. /**
  53. Priority to download and process image at.
  54. */
  55. typedef NS_ENUM(NSUInteger, PINRemoteImageManagerPriority) {
  56. /** Very low priority */
  57. PINRemoteImageManagerPriorityVeryLow = 0,
  58. /** Low priority */
  59. PINRemoteImageManagerPriorityLow,
  60. /** Medium priority */
  61. PINRemoteImageManagerPriorityMedium,
  62. /** High priority */
  63. PINRemoteImageManagerPriorityHigh,
  64. /** Very high priority */
  65. PINRemoteImageManagerPriorityVeryHigh,
  66. };
  67. NSOperationQueuePriority operationPriorityWithImageManagerPriority(PINRemoteImageManagerPriority priority);
  68. float dataTaskPriorityWithImageManagerPriority(PINRemoteImageManagerPriority priority);
  69. /**
  70. Completion called for many PINRemoteImage tasks as well as progress updates. Passed in a PINRemoteImageManagerResult.
  71. @param result PINRemoteImageManagerResult which contains the downloaded image.
  72. */
  73. typedef void (^PINRemoteImageManagerImageCompletion)(PINRemoteImageManagerResult * __nonnull result);
  74. /**
  75. Processor block to post-process a downloaded image. Passed in a PINRemoteImageManagerResult and a pointer to an NSUInteger which can be updated to indicate the cost of processing the image.
  76. @param result PINRemoteImageManagerResult which contains the downloaded image.
  77. @param cost NSUInteger point which can be modified to indicate the cost of processing the image. This is used when determining which cache objects to evict on memory pressure.
  78. @return return the processed UIImage
  79. */
  80. typedef PINImage * _Nullable(^PINRemoteImageManagerImageProcessor)(PINRemoteImageManagerResult * __nonnull result, NSUInteger * __nonnull cost);
  81. /**
  82. PINRemoteImageManager is the main workhorse of PINRemoteImage. It is unnecessary to access directly if you simply
  83. wish to download images and have them rendered in a UIImageView, UIButton or FLAnimatedImageView.
  84. However, if you wish to download images directly, this class is your guy / gal.
  85. You can use this class to download images, postprocess downloaded images, prefetch images, download images progressively, or download one image in a set of images depending on network performance.
  86. */
  87. /**
  88. Completion Handler block which will be forwarded to NSURLSessionTaskDelegate's completion handler
  89. @param disposition One of several constants that describes how the challenge should be handled.
  90. @param credential The credential that should be used for authentication if disposition is NSURLSessionAuthChallengeUseCredential; otherwise, NULL.
  91. */
  92. typedef void(^PINRemoteImageManagerAuthenticationChallengeCompletionHandler)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential);
  93. /**
  94. Authentication challenge handler
  95. @param task The task whose request requires authentication.
  96. @param challenge An object that contains the request for authentication.
  97. @param aHandler A PINRemoteImageManagerAuthenticationChallengeCompletionHandler, see example for further details.
  98. */
  99. typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * __nullable task, NSURLAuthenticationChallenge * __nonnull challenge, PINRemoteImageManagerAuthenticationChallengeCompletionHandler __nullable aHandler);
  100. /**
  101. Handler called for many PINRemoteImage tasks providing the progress of the download.
  102. @param completedBytes Amount of bytes that have been downloaded so far.
  103. @param totalBytes Total amount of bytes in the image being downloaded.
  104. */
  105. typedef void(^PINRemoteImageManagerProgressDownload)(int64_t completedBytes, int64_t totalBytes);
  106. /** An image downloading, processing and caching manager. It uses the concept of download and processing tasks to ensure that even if multiple calls to download or process an image are made, it only occurs one time (unless an item is no longer in the cache). PINRemoteImageManager is backed by GCD and safe to access from multiple threads simultaneously. It ensures that images are decoded off the main thread so that animation performance isn't affected. None of its exposed methods allow for synchronous access. However, it is optimized to call completions on the calling thread if an item is in its memory cache. **/
  107. @interface PINRemoteImageManager : NSObject
  108. @property (nonatomic, readonly, nonnull) id<PINRemoteImageCaching> cache;
  109. /**
  110. Create and return a PINRemoteImageManager created with the specified configuration. If configuration is nil, [NSURLSessionConfiguration defaultConfiguration] is used. Specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc.
  111. @param configuration The configuration used to create the PINRemoteImageManager.
  112. @return A PINRemoteImageManager with the specified configuration.
  113. */
  114. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration;
  115. /**
  116. Create and return a PINRemoteImageManager with the specified configuration and alternative representation delegate. If configuration is nil, [NSURLSessionConfiguration defaultConfiguration] is used. Specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc. If alternativeRepresentationProvider is nil, the default is used (and supports FLAnimatedImage).
  117. @param configuration The configuration used to create the PINRemoteImageManager.
  118. @param alternativeRepresentationProvider a delegate which conforms to the PINRemoteImageManagerAlternateRepresentationProvider protocol. Provide a delegate if you want to have non image results. @see PINRemoteImageManagerAlternateRepresentationProvider for an example.
  119. @return A PINRemoteImageManager with the specified configuration.
  120. */
  121. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration
  122. alternativeRepresentationProvider:(nullable id <PINRemoteImageManagerAlternateRepresentationProvider>)alternativeRepresentationProvider;
  123. /**
  124. Create and return a PINRemoteImageManager with the specified configuration and alternative representation delegate. If configuration is nil, [NSURLSessionConfiguration defaultConfiguration] is used. Specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc. If alternativeRepresentationProvider is nil, the default is used (and supports FLAnimatedImage).
  125. @param configuration The configuration used to create the PINRemoteImageManager.
  126. @param alternativeRepresentationProvider a delegate which conforms to the PINRemoteImageManagerAlternateRepresentationProvider protocol. Provide a delegate if you want to have non image results. @see PINRemoteImageManagerAlternateRepresentationProvider for an example.
  127. @param imageCache Optional delegate which conforms to the PINRemoteImageCaching protocol. Provide a delegate if you want to control image caching. By default, image manager will use most appropriate implementation available (based on PINCache or NSCache, depending on subspec)@see PINRemoteImageBasicCache for an example.
  128. @return A PINRemoteImageManager with the specified configuration.
  129. */
  130. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration
  131. alternativeRepresentationProvider:(nullable id <PINRemoteImageManagerAlternateRepresentationProvider>)alternativeRepresentationProvider
  132. imageCache:(nullable id<PINRemoteImageCaching>)imageCache NS_DESIGNATED_INITIALIZER;
  133. /**
  134. Get the shared instance of PINRemoteImageManager
  135. @return Shared instance of PINRemoteImageManager
  136. */
  137. + (nonnull instancetype)sharedImageManager;
  138. /**
  139. Sets the shared instance of PINRemoteImageManager to an instance with the supplied configuration. If configuration is nil, [NSURLSessionConfiguration ephemeralSessionConfiguration] is used. You specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc. This method should not be used if the shared instance has already been created.
  140. @param configuration The configuration used to create the PINRemoteImageManager.
  141. */
  142. + (void)setSharedImageManagerWithConfiguration:(nullable NSURLSessionConfiguration *)configuration;
  143. /**
  144. The result of this method is assigned to self.cache in init. If you wish to provide a customized cache to the manager you can subclass PINRemoteImageManager and return a custom object, implementing PINRemoteImageCaching protocol from this method. Same effect could be achieved by using initWithSessionConfiguration:alternativeRepresentationProvider:imageCache: initializer.
  145. @warning This method is meant only for override. It will be called *once* by an instance of PINRemoteImageManager. The default implementation creates a new cache on every call. If you're looking to access the cache being used by an instance of PINRemoteImageManager, @c cache.
  146. @return An instance of a object, implementing PINRemoteImageCaching protocol.
  147. */
  148. - (nonnull id<PINRemoteImageCaching>)defaultImageCache;
  149. /**
  150. * Sets a custom header to be included in every request. Headers set from this method will override any header from NSURLSessionConfiguration.
  151. *
  152. * @param value A value for the header. Pass in nil to remove a previously set value.
  153. * @param header A string field for header.
  154. */
  155. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)header;
  156. /**
  157. Set the Authentication Challenge Block.
  158. @param challengeBlock A PINRemoteImageManagerAuthenticationChallenge block.
  159. */
  160. - (void)setAuthenticationChallenge:(nullable PINRemoteImageManagerAuthenticationChallenge)challengeBlock;
  161. /**
  162. Set the minimum BPS to download the highest quality image in a set.
  163. @see downloadImageWithURLs:options:progressImage:completion:
  164. @param highQualityBPSThreshold bytes per second minimum. Defaults to 500000.
  165. @param completion Completion to be called once highQualityBPSThreshold has been set.
  166. */
  167. - (void)setHighQualityBPSThreshold:(float)highQualityBPSThreshold completion:(nullable dispatch_block_t)completion;
  168. /**
  169. Set the maximum BPS to download the lowest quality image in a set.
  170. @see downloadImageWithURLs:options:progressImage:completion:
  171. @param lowQualityBPSThreshold bytes per second maximum. Defaults to 50000.
  172. @param completion Completion to be called once lowQualityBPSThreshold has been set.
  173. */
  174. - (void)setLowQualityBPSThreshold:(float)lowQualityBPSThreshold
  175. completion:(nullable dispatch_block_t)completion;
  176. /**
  177. Set whether high quality images should be downloaded when a low quality image is cached if network connectivity has improved.
  178. @see downloadImageWithURLs:options:progressImage:completion:
  179. @param shouldUpgradeLowQualityImages if YES, low quality images will be 'upgraded'.
  180. @param completion Completion to be called once shouldUpgradeLowQualityImages has been set.
  181. */
  182. - (void)setShouldUpgradeLowQualityImages:(BOOL)shouldUpgradeLowQualityImages
  183. completion:(nullable dispatch_block_t)completion;
  184. /**
  185. Set the maximum number of concurrent operations (decompressing images, creating gifs, etc).
  186. @param maxNumberOfConcurrentOperations The maximum number of concurrent operations. Defaults to NSOperationQueueDefaultMaxConcurrentOperationCount.
  187. @param completion Completion to be called once maxNumberOfConcurrentOperations is set.
  188. */
  189. - (void)setMaxNumberOfConcurrentOperations:(NSInteger)maxNumberOfConcurrentOperations
  190. completion:(nullable dispatch_block_t)completion;
  191. /**
  192. Set the maximum number of concurrent downloads.
  193. @param maxNumberOfConcurrentDownloads The maximum number of concurrent downloads. Defaults to 10.
  194. @param completion Completion to be called once maxNumberOfConcurrentDownloads is set.
  195. */
  196. - (void)setMaxNumberOfConcurrentDownloads:(NSInteger)maxNumberOfConcurrentDownloads
  197. completion:(nullable dispatch_block_t)completion;
  198. /**
  199. Set the estimated time remaining to download threshold at which to generate progressive images. Progressive images previews will only be generated if the estimated remaining time on a download is greater than estimatedTimeRemainingThreshold. If estimatedTimeRemainingThreshold is less than or equal to zero, this check is skipped.
  200. @param estimatedRemainingTimeThreshold The estimated remaining time threshold used to decide to skip progressive rendering. Defaults to 0.1.
  201. @param completion Completion to be called once estimatedTimeRemainingTimeThreshold is set.
  202. */
  203. - (void)setEstimatedRemainingTimeThresholdForProgressiveDownloads:(NSTimeInterval)estimatedRemainingTimeThreshold
  204. completion:(nullable dispatch_block_t)completion;
  205. /**
  206. Sets the progress at which progressive images are generated. By default this is @[@0.00, @0.35, @0.65] which generates at most, 3 progressive images. The first progressive image will only be generated when at least one scan has been completed (so you never see half an image).
  207. @param progressThresholds an array of progress thresholds at which to generate progressive images. progress thresholds should range from 0.00 - 1.00. Defaults to @[@0.00, @0.35, @0.65]
  208. @param completion Completion to be called once progressThresholds is set.
  209. */
  210. - (void)setProgressThresholds:(nonnull NSArray <NSNumber *> *)progressThresholds
  211. completion:(nullable dispatch_block_t)completion;
  212. /**
  213. Sets whether PINRemoteImage should blur progressive render results
  214. @param shouldBlur A bool value indicating whether PINRemoteImage should blur progressive render results
  215. @param completion Completion to be called once progressThresholds is set.
  216. */
  217. - (void)setProgressiveRendersShouldBlur:(BOOL)shouldBlur
  218. completion:(nullable dispatch_block_t)completion;
  219. /**
  220. Sets the maximum size of an image that PINRemoteImage will render progessively. If the image is too large, progressive rendering is skipped.
  221. @param maxProgressiveRenderSize A CGSize which indicates the max size PINRemoteImage will render a progressive image. If an image is larger in either dimension, progressive rendering will be skipped
  222. @param completion Completion to be called once maxProgressiveRenderSize is set.
  223. */
  224. - (void)setProgressiveRendersMaxProgressiveRenderSize:(CGSize)maxProgressiveRenderSize
  225. completion:(nullable dispatch_block_t)completion;
  226. /**
  227. Prefetch an image at the given URL.
  228. @param url NSURL where the image to prefetch resides.
  229. */
  230. - (nullable NSUUID *)prefetchImageWithURL:(nonnull NSURL *)url;
  231. /**
  232. Prefetch an image at the given URL with given options.
  233. @param url NSURL where the image to prefetch resides.
  234. @param options PINRemoteImageManagerDownloadOptions options with which to pefetch the image.
  235. */
  236. - (nullable NSUUID *)prefetchImageWithURL:(nonnull NSURL *)url options:(PINRemoteImageManagerDownloadOptions)options;
  237. /**
  238. Prefetch images at the given URLs.
  239. @param urls An array of NSURLs where the images to prefetch reside.
  240. */
  241. - (nonnull NSArray<NSUUID *> *)prefetchImagesWithURLs:(nonnull NSArray <NSURL *> *)urls;
  242. /**
  243. Prefetch images at the given URLs with given options.
  244. @param urls An array of NSURLs where the images to prefetch reside.
  245. @param options PINRemoteImageManagerDownloadOptions options with which to pefetch the image.
  246. */
  247. - (nonnull NSArray<NSUUID *> *)prefetchImagesWithURLs:(nonnull NSArray <NSURL *> *)urls options:(PINRemoteImageManagerDownloadOptions)options;
  248. /**
  249. Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  250. @param url NSURL where the image to download resides.
  251. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  252. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  253. */
  254. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  255. /**
  256. Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  257. @param url NSURL where the image to download resides.
  258. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  259. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  260. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  261. */
  262. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  263. options:(PINRemoteImageManagerDownloadOptions)options
  264. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  265. /**
  266. Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  267. @param url NSURL where the image to download resides.
  268. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  269. @param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
  270. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  271. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  272. */
  273. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  274. options:(PINRemoteImageManagerDownloadOptions)options
  275. progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
  276. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  277. /**
  278. Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  279. @param url NSURL where the image to download resides.
  280. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  281. @param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
  282. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  283. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  284. */
  285. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  286. options:(PINRemoteImageManagerDownloadOptions)options
  287. progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
  288. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  289. /**
  290. Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  291. @param url NSURL where the image to download resides.
  292. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  293. @param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
  294. @param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
  295. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  296. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  297. */
  298. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  299. options:(PINRemoteImageManagerDownloadOptions)options
  300. progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
  301. progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
  302. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  303. /**
  304. Download or retrieve from cache the image found at the url and process it before calling completion. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  305. @param url NSURL where the image to download resides.
  306. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  307. @param processorKey NSString key to uniquely identify processor and process. Will be used for caching processed images.
  308. @param processor PINRemoteImageManagerImageProcessor block which will be called to post-process downloaded image.
  309. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  310. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  311. */
  312. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  313. options:(PINRemoteImageManagerDownloadOptions)options
  314. processorKey:(nullable NSString *)processorKey
  315. processor:(nullable PINRemoteImageManagerImageProcessor)processor
  316. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  317. /**
  318. Download or retrieve from cache the image found at the url and process it before calling completion. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  319. @param url NSURL where the image to download resides.
  320. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  321. @param processorKey NSString key to uniquely identify processor and process. Will be used for caching processed images.
  322. @param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
  323. @param processor PINRemoteImageManagerImageProcessor block which will be called to post-process downloaded image.
  324. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  325. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  326. */
  327. - (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
  328. options:(PINRemoteImageManagerDownloadOptions)options
  329. processorKey:(nullable NSString *)processorKey
  330. processor:(nullable PINRemoteImageManagerImageProcessor)processor
  331. progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
  332. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  333. /**
  334. Download or retrieve from cache one of the images found at the urls in the passed in array based on current network performance. URLs should be sorted from lowest quality image URL to highest. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
  335. Unless setShouldUpgradeLowQualityImages is set to YES, this method checks the cache for all URLs and returns the highest quality version stored. It is possible though unlikely for a cached image to not be returned if it is still being cached while a call is made to this method and if network conditions have changed. See source for more details.
  336. @param urls An array of NSURLs of increasing size.
  337. @param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
  338. @param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
  339. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
  340. @return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
  341. */
  342. - (nullable NSUUID *)downloadImageWithURLs:(nonnull NSArray <NSURL *> *)urls
  343. options:(PINRemoteImageManagerDownloadOptions)options
  344. progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
  345. completion:(nullable PINRemoteImageManagerImageCompletion)completion;
  346. /**
  347. Adds an image manually into the memory and disk cache.
  348. @param data NSData with the raw image data.
  349. @param url NSURL where the image resides.
  350. @param processorKey NSString key to uniquely identify processor and process. Will be used for caching processed images.
  351. @param additionalCost NSUInteger the additional cost (for cache eviction purposes) to generate the processed image
  352. @return A BOOL indicating if the image was successfully added to the cache.
  353. */
  354. - (BOOL) insertImageDataIntoCache:(nonnull NSData*)data
  355. withURL:(nonnull NSURL *)url
  356. processorKey:(nullable NSString *)processorKey
  357. additionalCost:(NSUInteger)additionalCost;
  358. /**
  359. Returns the cacheKey for a given URL and processorKey. Exposed to be overridden if necessary or to be used with imageFromCacheWithCacheKey
  360. @see imageFromCacheWithCacheKey:completion:
  361. @param url NSURL to be downloaded
  362. @param processorKey NSString key to uniquely identify processor and process.
  363. @return returns an NSString which is the key used for caching.
  364. */
  365. - (nonnull NSString *)cacheKeyForURL:(nonnull NSURL *)url processorKey:(nullable NSString *)processorKey;
  366. /**
  367. @see imageFromCacheWithURL:processorKey:options:completion:
  368. @deprecated
  369. @param cacheKey NSString key to look up image in the cache.
  370. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache.
  371. */
  372. - (void)imageFromCacheWithCacheKey:(nonnull NSString *)cacheKey completion:(nonnull PINRemoteImageManagerImageCompletion)completion __attribute__((deprecated));
  373. /**
  374. @see imageFromCacheWithURL:processorKey:options:completion:
  375. @deprecated
  376. @param cacheKey NSString key to look up image in the cache.
  377. @param options options will be used to determine if the cached image should be decompressed or FLAnimatedImages should be returned.
  378. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache.
  379. */
  380. - (void)imageFromCacheWithCacheKey:(nonnull NSString *)cacheKey options:(PINRemoteImageManagerDownloadOptions)options completion:(nonnull PINRemoteImageManagerImageCompletion)completion __attribute__((deprecated));
  381. /**
  382. Directly get an image from the underlying cache.
  383. @param url NSURL to look up image in the cache.
  384. @param processorKey NSString key to uniquely identify processor and process.
  385. @param options options will be used to determine if the cached image should be decompressed or FLAnimatedImages should be returned.
  386. @param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache.
  387. */
  388. - (void)imageFromCacheWithURL:(nonnull NSURL *)url processorKey:(nullable NSString *)processorKey options:(PINRemoteImageManagerDownloadOptions)options completion:(nonnull PINRemoteImageManagerImageCompletion)completion;
  389. /**
  390. @deprecated
  391. @see synchronousImageFromCacheWithURL:processorKey:options:
  392. @param cacheKey NSString key to look up image in the cache.
  393. @param options options will be used to determine if the cached image should be decompressed or FLAnimatedImages should be returned.
  394. @return A PINRemoteImageManagerResult
  395. */
  396. - (nonnull PINRemoteImageManagerResult *)synchronousImageFromCacheWithCacheKey:(nonnull NSString *)cacheKey options:(PINRemoteImageManagerDownloadOptions)options __attribute__((deprecated));
  397. /**
  398. Directly get an image from the underlying memory cache synchronously.
  399. @param url NSURL to look up image in the cache.
  400. @param processorKey NSString key to uniquely identify processor and process
  401. @param options options will be used to determine if the cached image should be decompressed or FLAnimatedImages should be returned.
  402. @return A PINRemoteImageManagerResult
  403. */
  404. - (nonnull PINRemoteImageManagerResult *)synchronousImageFromCacheWithURL:(nonnull NSURL *)url processorKey:(nullable NSString *)processorKey options:(PINRemoteImageManagerDownloadOptions)options;
  405. /**
  406. Cancel a download. Canceling will only cancel the download if all other downloads are also canceled with their associated UUIDs. Canceling *does not* guarantee that your completion will not be called. You can use the UUID provided on the result object verify the completion you want called is being called.
  407. @see PINRemoteImageCategoryManager
  408. @param UUID NSUUID of the task to cancel.
  409. */
  410. - (void)cancelTaskWithUUID:(nonnull NSUUID *)UUID;
  411. /**
  412. Set the priority of a download task. Since there is only one task per download, the priority of the download task will always be the last priority this method was called with.
  413. @param priority priority to set on the task.
  414. @param UUID NSUUID of the task to set the priority on.
  415. */
  416. - (void)setPriority:(PINRemoteImageManagerPriority)priority ofTaskWithUUID:(nonnull NSUUID *)UUID;
  417. /**
  418. * @abstract set the progress callback on a download task. You can use this to add progress callbacks or remove them for in flight downloads
  419. *
  420. * @param progressImageCallback a PINRemoteImageManagerImageCompletion block to be called with a progress update
  421. * @param UUID NSUUID of the task to set the priority on.
  422. */
  423. - (void)setProgressImageCallback:(nullable PINRemoteImageManagerImageCompletion)progressImageCallback ofTaskWithUUID:(nonnull NSUUID *)UUID;
  424. @end