ASMultiplexImageNode.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // ASMultiplexImageNode.h
  3. // AsyncDisplayKit
  4. //
  5. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  6. // This source code is licensed under the BSD-style license found in the
  7. // LICENSE file in the root directory of this source tree. An additional grant
  8. // of patent rights can be found in the PATENTS file in the same directory.
  9. //
  10. #if TARGET_OS_IOS
  11. #import <AsyncDisplayKit/ASImageNode.h>
  12. #import <AsyncDisplayKit/ASImageProtocols.h>
  13. #import <Photos/Photos.h>
  14. NS_ASSUME_NONNULL_BEGIN
  15. @protocol ASMultiplexImageNodeDelegate;
  16. @protocol ASMultiplexImageNodeDataSource;
  17. typedef id<NSCopying, NSObject> ASImageIdentifier;
  18. extern NSString *const ASMultiplexImageNodeErrorDomain;
  19. /**
  20. * ASMultiplexImageNode error codes.
  21. */
  22. typedef NS_ENUM(NSUInteger, ASMultiplexImageNodeErrorCode) {
  23. /**
  24. * Indicates that the data source didn't provide a source for an image identifier.
  25. */
  26. ASMultiplexImageNodeErrorCodeNoSourceForImage = 0,
  27. /**
  28. * Indicates that the best image identifier changed before a download for a worse identifier began.
  29. */
  30. ASMultiplexImageNodeErrorCodeBestImageIdentifierChanged,
  31. /**
  32. * Indicates that the Photos framework returned no image and no error.
  33. * This may happen if the image is in iCloud and the user did not specify `allowsNetworkAccess`
  34. * in their image request.
  35. */
  36. ASMultiplexImageNodeErrorCodePhotosImageManagerFailedWithoutError,
  37. /**
  38. * Indicates that the image node could not retrieve the PHAsset for a given asset identifier.
  39. * This typically means that the user has not given Photos framework permissions yet or the asset
  40. * has been removed from the device.
  41. */
  42. ASMultiplexImageNodeErrorCodePHAssetIsUnavailable
  43. };
  44. /**
  45. * @abstract ASMultiplexImageNode is an image node that can load and display multiple versions of an image. For
  46. * example, it can display a low-resolution version of an image while the high-resolution version is loading.
  47. *
  48. * @discussion ASMultiplexImageNode begins loading images when its resource can either return a UIImage directly, or a URL the image node should load.
  49. */
  50. @interface ASMultiplexImageNode : ASImageNode
  51. /**
  52. * @abstract The designated initializer.
  53. * @param cache The object that implements a cache of images for the image node.
  54. * @param downloader The object that implements image downloading for the image node.
  55. * @discussion If `cache` is nil, the receiver will not attempt to retrieve images from a cache before downloading them.
  56. * @return An initialized ASMultiplexImageNode.
  57. */
  58. - (instancetype)initWithCache:(nullable id<ASImageCacheProtocol>)cache downloader:(nullable id<ASImageDownloaderProtocol>)downloader NS_DESIGNATED_INITIALIZER;
  59. /**
  60. * @abstract The delegate, which must conform to the <ASMultiplexImageNodeDelegate> protocol.
  61. */
  62. @property (nonatomic, readwrite, weak) id <ASMultiplexImageNodeDelegate> delegate;
  63. /**
  64. * @abstract The data source, which must conform to the <ASMultiplexImageNodeDataSource> protocol.
  65. * @discussion This value is required for ASMultiplexImageNode to load images.
  66. */
  67. @property (nonatomic, readwrite, weak) id <ASMultiplexImageNodeDataSource> dataSource;
  68. /**
  69. * @abstract Whether the receiver should download more than just its highest-quality image. Defaults to NO.
  70. *
  71. * @discussion ASMultiplexImageNode immediately loads and displays the first image specified in <imageIdentifiers> (its
  72. * highest-quality image). If that image is not immediately available or cached, the node can download and display
  73. * lesser-quality images. Set `downloadsIntermediateImages` to YES to enable this behaviour.
  74. */
  75. @property (nonatomic, readwrite, assign) BOOL downloadsIntermediateImages;
  76. /**
  77. * @abstract An array of identifiers representing various versions of an image for ASMultiplexImageNode to display.
  78. *
  79. * @discussion An identifier can be any object that conforms to NSObject and NSCopying. The array should be in
  80. * decreasing order of image quality -- that is, the first identifier in the array represents the best version.
  81. *
  82. * @see <downloadsIntermediateImages> for more information on the image loading process.
  83. */
  84. @property (nonatomic, readwrite, copy) NSArray<ASImageIdentifier> *imageIdentifiers;
  85. /**
  86. * @abstract Notify the receiver SSAA that its data source has new UIImages or NSURLs available for <imageIdentifiers>.
  87. *
  88. * @discussion If a higher-quality image than is currently displayed is now available, it will be loaded.
  89. */
  90. - (void)reloadImageIdentifierSources;
  91. /**
  92. * @abstract The identifier for the last image that the receiver loaded, or nil.
  93. *
  94. * @discussion This value may differ from <displayedImageIdentifier> if the image hasn't yet been displayed.
  95. */
  96. @property (nullable, nonatomic, readonly) ASImageIdentifier loadedImageIdentifier;
  97. /**
  98. * @abstract The identifier for the image that the receiver is currently displaying, or nil.
  99. */
  100. @property (nullable, nonatomic, readonly) ASImageIdentifier displayedImageIdentifier;
  101. /**
  102. * @abstract If the downloader implements progressive image rendering and this value is YES progressive renders of the
  103. * image will be displayed as the image downloads. Regardless of this properties value, progress renders will
  104. * only occur when the node is visible. Defaults to YES.
  105. */
  106. @property (nonatomic, assign, readwrite) BOOL shouldRenderProgressImages;
  107. #if TARGET_OS_IOS
  108. /**
  109. * @abstract The image manager that this image node should use when requesting images from the Photos framework. If this is `nil` (the default), then `PHImageManager.defaultManager` is used.
  110. * @see `+[NSURL URLWithAssetLocalIdentifier:targetSize:contentMode:options:]` below.
  111. */
  112. @property (nullable, nonatomic, strong) PHImageManager *imageManager;
  113. #endif
  114. @end
  115. #pragma mark -
  116. /**
  117. * The methods declared by the ASMultiplexImageNodeDelegate protocol allow the adopting delegate to respond to
  118. * notifications such as began, progressed and finished downloading, updated and displayed an image.
  119. */
  120. @protocol ASMultiplexImageNodeDelegate <NSObject>
  121. @optional
  122. /**
  123. * @abstract Notification that the image node began downloading an image.
  124. * @param imageNode The sender.
  125. * @param imageIdentifier The identifier for the image that is downloading.
  126. */
  127. - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode didStartDownloadOfImageWithIdentifier:(id)imageIdentifier;
  128. /**
  129. * @abstract Notification that the image node's download progressed.
  130. * @param imageNode The sender.
  131. * @param downloadProgress The progress of the download. Value is between 0.0 and 1.0.
  132. * @param imageIdentifier The identifier for the image that is downloading.
  133. */
  134. - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode
  135. didUpdateDownloadProgress:(CGFloat)downloadProgress
  136. forImageWithIdentifier:(ASImageIdentifier)imageIdentifier;
  137. /**
  138. * @abstract Notification that the image node's download has finished.
  139. * @param imageNode The sender.
  140. * @param imageIdentifier The identifier for the image that finished downloading.
  141. * @param error The error that occurred while downloading, if one occurred; nil otherwise.
  142. */
  143. - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode
  144. didFinishDownloadingImageWithIdentifier:(ASImageIdentifier)imageIdentifier
  145. error:(nullable NSError *)error;
  146. /**
  147. * @abstract Notification that the image node's image was updated.
  148. * @param imageNode The sender.
  149. * @param image The new image, ready for display.
  150. * @param imageIdentifier The identifier for `image`.
  151. * @param previousImage The old, previously-loaded image.
  152. * @param previousImageIdentifier The identifier for `previousImage`.
  153. * @note This method does not indicate that `image` has been displayed.
  154. * @see <[ASMultiplexImageNodeDelegate multiplexImageNode:didDisplayUpdatedImage:withIdentifier:]>.
  155. */
  156. - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode
  157. didUpdateImage:(nullable UIImage *)image
  158. withIdentifier:(nullable ASImageIdentifier)imageIdentifier
  159. fromImage:(nullable UIImage *)previousImage
  160. withIdentifier:(nullable ASImageIdentifier)previousImageIdentifier;
  161. /**
  162. * @abstract Notification that the image node displayed a new image.
  163. * @param imageNode The sender.
  164. * @param image The new image, now being displayed.
  165. * @param imageIdentifier The identifier for `image`.
  166. * @discussion This method is only called when `image` changes, and not on subsequent redisplays of the same image.
  167. */
  168. - (void)multiplexImageNode:(ASMultiplexImageNode *)imageNode
  169. didDisplayUpdatedImage:(nullable UIImage *)image
  170. withIdentifier:(nullable ASImageIdentifier)imageIdentifier;
  171. /**
  172. * @abstract Notification that the image node finished displaying an image.
  173. * @param imageNode The sender.
  174. * @discussion This method is called every time an image is displayed, whether or not it has changed.
  175. */
  176. - (void)multiplexImageNodeDidFinishDisplay:(ASMultiplexImageNode *)imageNode;
  177. @end
  178. #pragma mark -
  179. /**
  180. * The ASMultiplexImageNodeDataSource protocol is adopted by an object that provides the multiplex image node,
  181. * for each image identifier, an image or a URL the image node should load.
  182. */
  183. @protocol ASMultiplexImageNodeDataSource <NSObject>
  184. @optional
  185. /**
  186. * @abstract An image for the specified identifier.
  187. * @param imageNode The sender.
  188. * @param imageIdentifier The identifier for the image that should be returned.
  189. * @discussion If the image is already available to the data source, this method should be used in lieu of providing the
  190. * URL to the image via -multiplexImageNode:URLForImageIdentifier:.
  191. * @return A UIImage corresponding to `imageIdentifier`, or nil if none is available.
  192. */
  193. - (nullable UIImage *)multiplexImageNode:(ASMultiplexImageNode *)imageNode imageForImageIdentifier:(ASImageIdentifier)imageIdentifier;
  194. /**
  195. * @abstract An image URL for the specified identifier.
  196. * @param imageNode The sender.
  197. * @param imageIdentifier The identifier for the image that will be downloaded.
  198. * @discussion Supported URLs include HTTP, HTTPS, AssetsLibrary, and FTP URLs as well as Photos framework URLs (see note).
  199. *
  200. * If the image is already available to the data source, it should be provided via <[ASMultiplexImageNodeDataSource
  201. * multiplexImageNode:imageForImageIdentifier:]> instead.
  202. * @return An NSURL for the image identified by `imageIdentifier`, or nil if none is available.
  203. * @see `+[NSURL URLWithAssetLocalIdentifier:targetSize:contentMode:options:]` below.
  204. */
  205. - (nullable NSURL *)multiplexImageNode:(ASMultiplexImageNode *)imageNode URLForImageIdentifier:(ASImageIdentifier)imageIdentifier;
  206. #if TARGET_OS_IOS
  207. /**
  208. * @abstract A PHAsset for the specific asset local identifier
  209. * @param imageNode The sender.
  210. * @param assetLocalIdentifier The local identifier for a PHAsset that this image node is loading.
  211. *
  212. * @discussion This optional method can improve image performance if your data source already has the PHAsset available.
  213. * If this method is not implemented, or returns nil, the image node will request the asset from the Photos framework.
  214. * @note This method may be called from any thread.
  215. * @return A PHAsset corresponding to `assetLocalIdentifier`, or nil if none is available.
  216. */
  217. - (nullable PHAsset *)multiplexImageNode:(ASMultiplexImageNode *)imageNode assetForLocalIdentifier:(NSString *)assetLocalIdentifier;
  218. #endif
  219. @end
  220. #pragma mark -
  221. #if TARGET_OS_IOS
  222. @interface NSURL (ASPhotosFrameworkURLs)
  223. /**
  224. * @abstract Create an NSURL that specifies an image from the Photos framework.
  225. *
  226. * @discussion When implementing `-multiplexImageNode:URLForImageIdentifier:`, you can return a URL
  227. * created by this method and the image node will attempt to load the image from the Photos framework.
  228. * @note The `synchronous` flag in `options` is ignored.
  229. * @note The `Opportunistic` delivery mode is not supported and will be treated as `HighQualityFormat`.
  230. */
  231. + (NSURL *)URLWithAssetLocalIdentifier:(NSString *)assetLocalIdentifier
  232. targetSize:(CGSize)targetSize
  233. contentMode:(PHImageContentMode)contentMode
  234. options:(PHImageRequestOptions *)options AS_WARN_UNUSED_RESULT;
  235. @end
  236. #endif
  237. NS_ASSUME_NONNULL_END
  238. #endif