SDImageCoder.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import "SDWebImageCompat.h"
  10. #import "NSData+ImageContentType.h"
  11. typedef NSString * SDImageCoderOption NS_STRING_ENUM;
  12. typedef NSDictionary<SDImageCoderOption, id> SDImageCoderOptions;
  13. typedef NSMutableDictionary<SDImageCoderOption, id> SDImageCoderMutableOptions;
  14. #pragma mark - Coder Options
  15. // These options are for image decoding
  16. /**
  17. A Boolean value indicating whether to decode the first frame only for animated image during decoding. (NSNumber). If not provide, decode animated image if need.
  18. @note works for `SDImageCoder`.
  19. */
  20. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeFirstFrameOnly;
  21. /**
  22. A CGFloat value which is greater than or equal to 1.0. This value specify the image scale factor for decoding. If not provide, use 1.0. (NSNumber)
  23. @note works for `SDImageCoder`, `SDProgressiveImageCoder`, `SDAnimatedImageCoder`.
  24. */
  25. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeScaleFactor;
  26. /**
  27. A Boolean value indicating whether to keep the original aspect ratio when generating thumbnail images (or bitmap images from vector format).
  28. Defaults to YES.
  29. @note works for `SDImageCoder`, `SDProgressiveImageCoder`, `SDAnimatedImageCoder`.
  30. */
  31. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodePreserveAspectRatio;
  32. /**
  33. A CGSize value indicating whether or not to generate the thumbnail images (or bitmap images from vector format). When this value is provided, the decoder will generate a thumbnail image which pixel size is smaller than or equal to (depends the `.preserveAspectRatio`) the value size.
  34. Defaults to CGSizeZero, which means no thumbnail generation at all.
  35. @note Supports for animated image as well.
  36. @note When you pass `.preserveAspectRatio == NO`, the thumbnail image is stretched to match each dimension. When `.preserveAspectRatio == YES`, the thumbnail image's width is limited to pixel size's width, the thumbnail image's height is limited to pixel size's height. For common cases, you can just pass a square size to limit both.
  37. @note works for `SDImageCoder`, `SDProgressiveImageCoder`, `SDAnimatedImageCoder`.
  38. */
  39. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderDecodeThumbnailPixelSize;
  40. // These options are for image encoding
  41. /**
  42. A Boolean value indicating whether to encode the first frame only for animated image during encoding. (NSNumber). If not provide, encode animated image if need.
  43. @note works for `SDImageCoder`.
  44. */
  45. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeFirstFrameOnly;
  46. /**
  47. A double value between 0.0-1.0 indicating the encode compression quality to produce the image data. 1.0 resulting in no compression and 0.0 resulting in the maximum compression possible. If not provide, use 1.0. (NSNumber)
  48. @note works for `SDImageCoder`
  49. */
  50. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeCompressionQuality;
  51. /**
  52. A UIColor(NSColor) value to used for non-alpha image encoding when the input image has alpha channel, the background color will be used to compose the alpha one. If not provide, use white color.
  53. @note works for `SDImageCoder`
  54. */
  55. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeBackgroundColor;
  56. /**
  57. A CGSize value indicating the max image resolution in pixels during encoding. For vector image, this also effect the output vector data information about width and height. The encoder will not generate the encoded image larger than this limit. Note it always use the aspect ratio of input image..
  58. Defaults to CGSizeZero, which means no max size limit at all.
  59. @note Supports for animated image as well.
  60. @note The output image's width is limited to pixel size's width, the output image's height is limited to pixel size's height. For common cases, you can just pass a square size to limit both.
  61. @note works for `SDImageCoder`
  62. */
  63. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxPixelSize;
  64. /**
  65. A NSUInteger value specify the max output data bytes size after encoding. Some lossy format like JPEG/HEIF supports the hint for codec to automatically reduce the quality and match the file size you want. Note this option will override the `SDImageCoderEncodeCompressionQuality`, because now the quality is decided by the encoder. (NSNumber)
  66. @note This is a hint, no guarantee for output size because of compression algorithm limit. And this options does not works for vector images.
  67. @note works for `SDImageCoder`
  68. */
  69. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxFileSize;
  70. /**
  71. A Boolean value indicating the encoding format should contains a thumbnail image into the output data. Only some of image format (like JPEG/HEIF/AVIF) support this behavior. The embed thumbnail will be used during next time thumbnail decoding (provided `.thumbnailPixelSize`), which is faster than full image thumbnail decoding. (NSNumber)
  72. Defaults to NO, which does not embed any thumbnail.
  73. @note The thumbnail image's pixel size is not defined, the encoder can choose the proper pixel size which is suitable for encoding quality.
  74. @note works for `SDImageCoder`
  75. */
  76. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeEmbedThumbnail;
  77. /**
  78. A SDWebImageContext object which hold the original context options from top-level API. (SDWebImageContext)
  79. This option is ignored for all built-in coders and take no effect.
  80. But this may be useful for some custom coders, because some business logic may dependent on things other than image or image data information only.
  81. See `SDWebImageContext` for more detailed information.
  82. */
  83. FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderWebImageContext API_DEPRECATED("The coder component will be seperated from Core subspec in the future. Update your code to not rely on this context option.", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));;
  84. #pragma mark - Coder
  85. /**
  86. This is the image coder protocol to provide custom image decoding/encoding.
  87. These methods are all required to implement.
  88. @note Pay attention that these methods are not called from main queue.
  89. */
  90. @protocol SDImageCoder <NSObject>
  91. @required
  92. #pragma mark - Decoding
  93. /**
  94. Returns YES if this coder can decode some data. Otherwise, the data should be passed to another coder.
  95. @param data The image data so we can look at it
  96. @return YES if this coder can decode the data, NO otherwise
  97. */
  98. - (BOOL)canDecodeFromData:(nullable NSData *)data;
  99. /**
  100. Decode the image data to image.
  101. @note This protocol may supports decode animated image frames. You can use `+[SDImageCoderHelper animatedImageWithFrames:]` to produce an animated image with frames.
  102. @param data The image data to be decoded
  103. @param options A dictionary containing any decoding options. Pass @{SDImageCoderDecodeScaleFactor: @(1.0)} to specify scale factor for image. Pass @{SDImageCoderDecodeFirstFrameOnly: @(YES)} to decode the first frame only.
  104. @return The decoded image from data
  105. */
  106. - (nullable UIImage *)decodedImageWithData:(nullable NSData *)data
  107. options:(nullable SDImageCoderOptions *)options;
  108. #pragma mark - Encoding
  109. /**
  110. Returns YES if this coder can encode some image. Otherwise, it should be passed to another coder.
  111. For custom coder which introduce new image format, you'd better define a new `SDImageFormat` using like this. If you're creating public coder plugin for new image format, also update `https://github.com/rs/SDWebImage/wiki/Coder-Plugin-List` to avoid same value been defined twice.
  112. * @code
  113. static const SDImageFormat SDImageFormatHEIF = 10;
  114. * @endcode
  115. @param format The image format
  116. @return YES if this coder can encode the image, NO otherwise
  117. */
  118. - (BOOL)canEncodeToFormat:(SDImageFormat)format NS_SWIFT_NAME(canEncode(to:));
  119. /**
  120. Encode the image to image data.
  121. @note This protocol may supports encode animated image frames. You can use `+[SDImageCoderHelper framesFromAnimatedImage:]` to assemble an animated image with frames.
  122. @param image The image to be encoded
  123. @param format The image format to encode, you should note `SDImageFormatUndefined` format is also possible
  124. @param options A dictionary containing any encoding options. Pass @{SDImageCoderEncodeCompressionQuality: @(1)} to specify compression quality.
  125. @return The encoded image data
  126. */
  127. - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image
  128. format:(SDImageFormat)format
  129. options:(nullable SDImageCoderOptions *)options;
  130. @end
  131. #pragma mark - Progressive Coder
  132. /**
  133. This is the image coder protocol to provide custom progressive image decoding.
  134. These methods are all required to implement.
  135. @note Pay attention that these methods are not called from main queue.
  136. */
  137. @protocol SDProgressiveImageCoder <SDImageCoder>
  138. @required
  139. /**
  140. Returns YES if this coder can incremental decode some data. Otherwise, it should be passed to another coder.
  141. @param data The image data so we can look at it
  142. @return YES if this coder can decode the data, NO otherwise
  143. */
  144. - (BOOL)canIncrementalDecodeFromData:(nullable NSData *)data;
  145. /**
  146. Because incremental decoding need to keep the decoded context, we will alloc a new instance with the same class for each download operation to avoid conflicts
  147. This init method should not return nil
  148. @param options A dictionary containing any progressive decoding options (instance-level). Pass @{SDImageCoderDecodeScaleFactor: @(1.0)} to specify scale factor for progressive animated image (each frames should use the same scale).
  149. @return A new instance to do incremental decoding for the specify image format
  150. */
  151. - (nonnull instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)options;
  152. /**
  153. Update the incremental decoding when new image data available
  154. @param data The image data has been downloaded so far
  155. @param finished Whether the download has finished
  156. */
  157. - (void)updateIncrementalData:(nullable NSData *)data finished:(BOOL)finished;
  158. /**
  159. Incremental decode the current image data to image.
  160. @note Due to the performance issue for progressive decoding and the integration for image view. This method may only return the first frame image even if the image data is animated image. If you want progressive animated image decoding, conform to `SDAnimatedImageCoder` protocol as well and use `animatedImageFrameAtIndex:` instead.
  161. @param options A dictionary containing any progressive decoding options. Pass @{SDImageCoderDecodeScaleFactor: @(1.0)} to specify scale factor for progressive image
  162. @return The decoded image from current data
  163. */
  164. - (nullable UIImage *)incrementalDecodedImageWithOptions:(nullable SDImageCoderOptions *)options;
  165. @end
  166. #pragma mark - Animated Image Provider
  167. /**
  168. This is the animated image protocol to provide the basic function for animated image rendering. It's adopted by `SDAnimatedImage` and `SDAnimatedImageCoder`
  169. */
  170. @protocol SDAnimatedImageProvider <NSObject>
  171. @required
  172. /**
  173. The original animated image data for current image. If current image is not an animated format, return nil.
  174. We may use this method to grab back the original image data if need, such as NSCoding or compare.
  175. @return The animated image data
  176. */
  177. @property (nonatomic, copy, readonly, nullable) NSData *animatedImageData;
  178. /**
  179. Total animated frame count.
  180. If the frame count is less than 1, then the methods below will be ignored.
  181. @return Total animated frame count.
  182. */
  183. @property (nonatomic, assign, readonly) NSUInteger animatedImageFrameCount;
  184. /**
  185. Animation loop count, 0 means infinite looping.
  186. @return Animation loop count
  187. */
  188. @property (nonatomic, assign, readonly) NSUInteger animatedImageLoopCount;
  189. /**
  190. Returns the frame image from a specified index.
  191. @note The index maybe randomly if one image was set to different imageViews, keep it re-entrant. (It's not recommend to store the images into array because it's memory consuming)
  192. @param index Frame index (zero based).
  193. @return Frame's image
  194. */
  195. - (nullable UIImage *)animatedImageFrameAtIndex:(NSUInteger)index;
  196. /**
  197. Returns the frames's duration from a specified index.
  198. @note The index maybe randomly if one image was set to different imageViews, keep it re-entrant. (It's recommend to store the durations into array because it's not memory-consuming)
  199. @param index Frame index (zero based).
  200. @return Frame's duration
  201. */
  202. - (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index;
  203. @end
  204. #pragma mark - Animated Coder
  205. /**
  206. This is the animated image coder protocol for custom animated image class like `SDAnimatedImage`. Through it inherit from `SDImageCoder`. We currentlly only use the method `canDecodeFromData:` to detect the proper coder for specify animated image format.
  207. */
  208. @protocol SDAnimatedImageCoder <SDImageCoder, SDAnimatedImageProvider>
  209. @required
  210. /**
  211. Because animated image coder should keep the original data, we will alloc a new instance with the same class for the specify animated image data
  212. The init method should return nil if it can't decode the specify animated image data to produce any frame.
  213. After the instance created, we may call methods in `SDAnimatedImageProvider` to produce animated image frame.
  214. @param data The animated image data to be decode
  215. @param options A dictionary containing any animated decoding options (instance-level). Pass @{SDImageCoderDecodeScaleFactor: @(1.0)} to specify scale factor for animated image (each frames should use the same scale).
  216. @return A new instance to do animated decoding for specify image data
  217. */
  218. - (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data options:(nullable SDImageCoderOptions *)options;
  219. @end