ASImageNode.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // ASImageNode.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. #import <UIKit/UIKit.h>
  11. #import <AsyncDisplayKit/ASControlNode.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. @protocol ASAnimatedImageProtocol;
  14. /**
  15. * Image modification block. Use to transform an image before display.
  16. *
  17. * @param image The image to be displayed.
  18. *
  19. * @return A transformed image.
  20. */
  21. typedef UIImage * _Nullable (^asimagenode_modification_block_t)(UIImage *image);
  22. /**
  23. * @abstract Draws images.
  24. * @discussion Supports cropping, tinting, and arbitrary image modification blocks.
  25. */
  26. @interface ASImageNode : ASControlNode
  27. /**
  28. * @abstract The image to display.
  29. *
  30. * @discussion The node will efficiently display stretchable images by using
  31. * the layer's contentsCenter property. Non-stretchable images work too, of
  32. * course.
  33. */
  34. @property (nullable, nonatomic, strong) UIImage *image;
  35. /**
  36. @abstract The placeholder color.
  37. */
  38. @property (nullable, nonatomic, strong) UIColor *placeholderColor;
  39. /**
  40. * @abstract Indicates whether efficient cropping of the receiver is enabled.
  41. *
  42. * @discussion Defaults to YES. See -setCropEnabled:recropImmediately:inBounds: for more
  43. * information.
  44. */
  45. @property (nonatomic, assign, getter=isCropEnabled) BOOL cropEnabled;
  46. /**
  47. * @abstract Indicates that efficient downsizing of backing store should *not* be enabled.
  48. *
  49. * @discussion Defaults to NO. @see ASCroppedImageBackingSizeAndDrawRectInBounds for more
  50. * information.
  51. */
  52. @property (nonatomic, assign) BOOL forceUpscaling;
  53. /**
  54. * @abstract Forces image to be rendered at forcedSize.
  55. * @discussion Defaults to CGSizeZero to indicate that the forcedSize should not be used.
  56. * Setting forcedSize to non-CGSizeZero will force the backing of the layer contents to
  57. * be forcedSize (automatically adjusted for contentsSize).
  58. */
  59. @property (nonatomic, assign) CGSize forcedSize;
  60. /**
  61. * @abstract Enables or disables efficient cropping.
  62. *
  63. * @param cropEnabled YES to efficiently crop the receiver's contents such that
  64. * contents outside of its bounds are not included; NO otherwise.
  65. *
  66. * @param recropImmediately If the receiver has an image, YES to redisplay the
  67. * receiver immediately; NO otherwise.
  68. *
  69. * @param cropBounds The bounds into which the receiver will be cropped. Useful
  70. * if bounds are to change in response to cropping (but have not yet done so).
  71. *
  72. * @discussion Efficient cropping is only performed when the receiver's view's
  73. * contentMode is UIViewContentModeScaleAspectFill. By default, cropping is
  74. * enabled. The crop alignment may be controlled via cropAlignmentFactor.
  75. */
  76. - (void)setCropEnabled:(BOOL)cropEnabled recropImmediately:(BOOL)recropImmediately inBounds:(CGRect)cropBounds;
  77. /**
  78. * @abstract A value that controls how the receiver's efficient cropping is aligned.
  79. *
  80. * @discussion This value defines a rectangle that is to be featured by the
  81. * receiver. The rectangle is specified as a "unit rectangle," using
  82. * fractions of the source image's width and height, e.g. CGRectMake(0.5, 0,
  83. * 0.5, 1.0) will feature the full right half a photo. If the cropRect is
  84. * empty, the content mode of the receiver will be used to determine its
  85. * dimensions, and only the cropRect's origin will be used for positioning. The
  86. * default value of this property is CGRectMake(0.5, 0.5, 0.0, 0.0).
  87. */
  88. @property (nonatomic, readwrite, assign) CGRect cropRect;
  89. /**
  90. * @abstract An optional block which can perform drawing operations on image
  91. * during the display phase.
  92. *
  93. * @discussion Can be used to add image effects (such as rounding, adding
  94. * borders, or other pattern overlays) without extraneous display calls.
  95. */
  96. @property (nullable, nonatomic, readwrite, copy) asimagenode_modification_block_t imageModificationBlock;
  97. /**
  98. * @abstract Marks the receiver as needing display and performs a block after
  99. * display has finished.
  100. *
  101. * @param displayCompletionBlock The block to be performed after display has
  102. * finished. Its `canceled` property will be YES if display was prevented or
  103. * canceled (via displaySuspended); NO otherwise.
  104. *
  105. * @discussion displayCompletionBlock will be performed on the main-thread. If
  106. * `displaySuspended` is YES, `displayCompletionBlock` is will be
  107. * performed immediately and `YES` will be passed for `canceled`.
  108. */
  109. - (void)setNeedsDisplayWithCompletion:(nullable void (^)(BOOL canceled))displayCompletionBlock;
  110. #if TARGET_OS_TV
  111. /**
  112. * A bool to track if the current appearance of the node
  113. * is the default focus appearance.
  114. * Exposed here so the category methods can set it.
  115. */
  116. @property (nonatomic, assign) BOOL isDefaultFocusAppearance;
  117. #endif
  118. @end
  119. @interface ASImageNode (AnimatedImage)
  120. /**
  121. * @abstract The animated image to playback
  122. *
  123. * @discussion Set this to an object which conforms to ASAnimatedImageProtocol
  124. * to have the ASImageNode playback an animated image.
  125. */
  126. @property (nullable, nonatomic, strong) id <ASAnimatedImageProtocol> animatedImage;
  127. /**
  128. * @abstract Pause the playback of an animated image.
  129. *
  130. * @discussion Set to YES to pause playback of an animated image and NO to resume
  131. * playback.
  132. */
  133. @property (nonatomic, assign) BOOL animatedImagePaused;
  134. /**
  135. * @abstract The runloop mode used to animate the image.
  136. *
  137. * @discussion Defaults to NSRunLoopCommonModes. Another commonly used mode is NSDefaultRunLoopMode.
  138. * Setting NSDefaultRunLoopMode will cause animation to pause while scrolling (if the ASImageNode is
  139. * in a scroll view), which may improve scroll performance in some use cases.
  140. */
  141. @property (nonatomic, strong) NSString *animatedImageRunLoopMode;
  142. @end
  143. @interface ASImageNode (Unavailable)
  144. - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock AS_UNAVAILABLE();
  145. - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock AS_UNAVAILABLE();
  146. @end
  147. ASDISPLAYNODE_EXTERN_C_BEGIN
  148. /**
  149. * @abstract Image modification block that rounds (and optionally adds a border to) an image.
  150. *
  151. * @param borderWidth The width of the round border to draw, or zero if no border is desired.
  152. * @param borderColor What colour border to draw.
  153. *
  154. * @see <imageModificationBlock>
  155. *
  156. * @return An ASImageNode image modification block.
  157. */
  158. asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor * _Nullable borderColor);
  159. /**
  160. * @abstract Image modification block that applies a tint color à la UIImage configured with
  161. * renderingMode set to UIImageRenderingModeAlwaysTemplate.
  162. *
  163. * @param color The color to tint the image.
  164. *
  165. * @see <imageModificationBlock>
  166. *
  167. * @return An ASImageNode image modification block.
  168. */
  169. asimagenode_modification_block_t ASImageNodeTintColorModificationBlock(UIColor *color);
  170. ASDISPLAYNODE_EXTERN_C_END
  171. NS_ASSUME_NONNULL_END