SDWebImageCoder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  12. A Boolean value indicating whether to scale down large images during decompressing. (NSNumber)
  13. */
  14. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageCoderScaleDownLargeImagesKey;
  15. /**
  16. Return the shared device-dependent RGB color space created with CGColorSpaceCreateDeviceRGB.
  17. @return The device-dependent RGB color space
  18. */
  19. CG_EXTERN CGColorSpaceRef _Nonnull SDCGColorSpaceGetDeviceRGB(void);
  20. /**
  21. Check whether CGImageRef contains alpha channel.
  22. @param imageRef The CGImageRef
  23. @return Return YES if CGImageRef contains alpha channel, otherwise return NO
  24. */
  25. CG_EXTERN BOOL SDCGImageRefContainsAlpha(_Nullable CGImageRef imageRef);
  26. /**
  27. This is the image coder protocol to provide custom image decoding/encoding.
  28. These methods are all required to implement.
  29. @note Pay attention that these methods are not called from main queue.
  30. */
  31. @protocol SDWebImageCoder <NSObject>
  32. @required
  33. #pragma mark - Decoding
  34. /**
  35. Returns YES if this coder can decode some data. Otherwise, the data should be passed to another coder.
  36. @param data The image data so we can look at it
  37. @return YES if this coder can decode the data, NO otherwise
  38. */
  39. - (BOOL)canDecodeFromData:(nullable NSData *)data;
  40. /**
  41. Decode the image data to image.
  42. @param data The image data to be decoded
  43. @return The decoded image from data
  44. */
  45. - (nullable UIImage *)decodedImageWithData:(nullable NSData *)data;
  46. /**
  47. Decompress the image with original image and image data.
  48. @param image The original image to be decompressed
  49. @param data The pointer to original image data. The pointer itself is nonnull but image data can be null. This data will set to cache if needed. If you do not need to modify data at the sametime, ignore this param.
  50. @param optionsDict A dictionary containing any decompressing options. Pass {SDWebImageCoderScaleDownLargeImagesKey: @(YES)} to scale down large images
  51. @return The decompressed image
  52. */
  53. - (nullable UIImage *)decompressedImageWithImage:(nullable UIImage *)image
  54. data:(NSData * _Nullable * _Nonnull)data
  55. options:(nullable NSDictionary<NSString*, NSObject*>*)optionsDict;
  56. #pragma mark - Encoding
  57. /**
  58. Returns YES if this coder can encode some image. Otherwise, it should be passed to another coder.
  59. @param format The image format
  60. @return YES if this coder can encode the image, NO otherwise
  61. */
  62. - (BOOL)canEncodeToFormat:(SDImageFormat)format;
  63. /**
  64. Encode the image to image data.
  65. @param image The image to be encoded
  66. @param format The image format to encode, you should note `SDImageFormatUndefined` format is also possible
  67. @return The encoded image data
  68. */
  69. - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDImageFormat)format;
  70. @end
  71. /**
  72. This is the image coder protocol to provide custom progressive image decoding.
  73. These methods are all required to implement.
  74. @note Pay attention that these methods are not called from main queue.
  75. */
  76. @protocol SDWebImageProgressiveCoder <SDWebImageCoder>
  77. @required
  78. /**
  79. Returns YES if this coder can incremental decode some data. Otherwise, it should be passed to another coder.
  80. @param data The image data so we can look at it
  81. @return YES if this coder can decode the data, NO otherwise
  82. */
  83. - (BOOL)canIncrementallyDecodeFromData:(nullable NSData *)data;
  84. /**
  85. Incremental decode the image data to image.
  86. @param data The image data has been downloaded so far
  87. @param finished Whether the download has finished
  88. @warning 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
  89. @return The decoded image from data
  90. */
  91. - (nullable UIImage *)incrementallyDecodedImageWithData:(nullable NSData *)data finished:(BOOL)finished;
  92. @end