UIView+WebCache.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "SDWebImageCompat.h"
  9. #import "SDWebImageManager.h"
  10. #import "SDWebImageTransition.h"
  11. /**
  12. A Dispatch group to maintain setImageBlock and completionBlock. This key should be used only internally and may be changed in the future. (dispatch_group_t)
  13. */
  14. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageInternalSetImageGroupKey;
  15. /**
  16. A SDWebImageManager instance to control the image download and cache process using in UIImageView+WebCache category and likes. If not provided, use the shared manager (SDWebImageManager)
  17. */
  18. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageExternalCustomManagerKey;
  19. /**
  20. The value specify that the image progress unit count cannot be determined because the progressBlock is not been called.
  21. */
  22. FOUNDATION_EXPORT const int64_t SDWebImageProgressUnitCountUnknown; /* 1LL */
  23. typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable imageData);
  24. typedef void(^SDInternalSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
  25. @interface UIView (WebCache)
  26. /**
  27. * Get the current image URL.
  28. *
  29. * @note Note that because of the limitations of categories this property can get out of sync if you use setImage: directly.
  30. */
  31. - (nullable NSURL *)sd_imageURL;
  32. /**
  33. * The current image loading progress associated to the view. The unit count is the received size and excepted size of download.
  34. * The `totalUnitCount` and `completedUnitCount` will be reset to 0 after a new image loading start (change from current queue). And they will be set to `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).
  35. * @note You can use Key-Value Observing on the progress, but you should take care that the change to progress is from a background queue during download(the same as progressBlock). If you want to using KVO and update the UI, make sure to dispatch on the main queue. And it's recommand to use some KVO libs like KVOController because it's more safe and easy to use.
  36. * @note The getter will create a progress instance if the value is nil. You can also set a custom progress instance and let it been updated during image loading
  37. * @note Note that because of the limitations of categories this property can get out of sync if you update the progress directly.
  38. */
  39. @property (nonatomic, strong, null_resettable) NSProgress *sd_imageProgress;
  40. /**
  41. * Set the imageView `image` with an `url` and optionally a placeholder image.
  42. *
  43. * The download is asynchronous and cached.
  44. *
  45. * @param url The url for the image.
  46. * @param placeholder The image to be set initially, until the image request finishes.
  47. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  48. * @param operationKey A string to be used as the operation key. If nil, will use the class name
  49. * @param setImageBlock Block used for custom set image code
  50. * @param progressBlock A block called while image is downloading
  51. * @note the progress block is executed on a background queue
  52. * @param completedBlock A block called when operation has been completed. This block has no return value
  53. * and takes the requested UIImage as first parameter. In case of error the image parameter
  54. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  55. * indicating if the image was retrieved from the local cache or from the network.
  56. * The fourth parameter is the original image url.
  57. */
  58. - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
  59. placeholderImage:(nullable UIImage *)placeholder
  60. options:(SDWebImageOptions)options
  61. operationKey:(nullable NSString *)operationKey
  62. setImageBlock:(nullable SDSetImageBlock)setImageBlock
  63. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  64. completed:(nullable SDExternalCompletionBlock)completedBlock;
  65. /**
  66. * Set the imageView `image` with an `url` and optionally a placeholder image.
  67. *
  68. * The download is asynchronous and cached.
  69. *
  70. * @param url The url for the image.
  71. * @param placeholder The image to be set initially, until the image request finishes.
  72. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  73. * @param operationKey A string to be used as the operation key. If nil, will use the class name
  74. * @param setImageBlock Block used for custom set image code
  75. * @param progressBlock A block called while image is downloading
  76. * @note the progress block is executed on a background queue
  77. * @param completedBlock A block called when operation has been completed. This block has no return value
  78. * and takes the requested UIImage as first parameter. In case of error the image parameter
  79. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  80. * indicating if the image was retrieved from the local cache or from the network.
  81. * The fourth parameter is the original image url.
  82. * @param context A context with extra information to perform specify changes or processes.
  83. */
  84. - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
  85. placeholderImage:(nullable UIImage *)placeholder
  86. options:(SDWebImageOptions)options
  87. operationKey:(nullable NSString *)operationKey
  88. setImageBlock:(nullable SDSetImageBlock)setImageBlock
  89. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  90. completed:(nullable SDExternalCompletionBlock)completedBlock
  91. context:(nullable NSDictionary<NSString *, id> *)context;
  92. /**
  93. * Cancel the current image load
  94. */
  95. - (void)sd_cancelCurrentImageLoad;
  96. #if SD_UIKIT || SD_MAC
  97. #pragma mark - Image Transition
  98. /**
  99. The image transition when image load finished. See `SDWebImageTransition`.
  100. If you specify nil, do not do transition. Defautls to nil.
  101. */
  102. @property (nonatomic, strong, nullable) SDWebImageTransition *sd_imageTransition;
  103. #if SD_UIKIT
  104. #pragma mark - Activity indicator
  105. /**
  106. * Show activity UIActivityIndicatorView
  107. */
  108. - (void)sd_setShowActivityIndicatorView:(BOOL)show;
  109. /**
  110. * set desired UIActivityIndicatorViewStyle
  111. *
  112. * @param style The style of the UIActivityIndicatorView
  113. */
  114. - (void)sd_setIndicatorStyle:(UIActivityIndicatorViewStyle)style;
  115. - (BOOL)sd_showActivityIndicatorView;
  116. - (void)sd_addActivityIndicator;
  117. - (void)sd_removeActivityIndicator;
  118. #endif
  119. #endif
  120. @end