UIView+BUWebCache.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "BU_SDWebImageCompat.h"
  9. #import "BU_SDWebImageDefine.h"
  10. #import "BU_SDWebImageManager.h"
  11. #import "BU_SDWebImageTransition.h"
  12. #import "BU_SDWebImageIndicator.h"
  13. /**
  14. The value specify that the image progress unit count cannot be determined because the progressBlock is not been called.
  15. */
  16. FOUNDATION_EXPORT const int64_t BU_SDWebImageProgressUnitCountUnknown; /* 1LL */
  17. typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable imageData, BU_SDImageCacheType cacheType, NSURL * _Nullable imageURL);
  18. /**
  19. Integrates SDWebImage async downloading and caching of remote images with UIView subclass.
  20. */
  21. @interface UIView (BUWebCache)
  22. /**
  23. * Get the current image URL.
  24. *
  25. * @note Note that because of the limitations of categories this property can get out of sync if you use setImage: directly.
  26. */
  27. @property (nonatomic, strong, readonly, nullable) NSURL *sdBu_imageURL;
  28. /**
  29. * The current image loading progress associated to the view. The unit count is the received size and excepted size of download.
  30. * 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 `BU_SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).
  31. * @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.
  32. * @note The getter will create a progress instance if the value is nil. But by default, we don't create one. If you need to use Key-Value Observing, you must trigger the getter or set a custom progresss instance before the loading start. The default value is nil.
  33. * @note Note that because of the limitations of categories this property can get out of sync if you update the progress directly.
  34. */
  35. @property (nonatomic, strong, null_resettable) NSProgress *sdBu_imageProgress;
  36. /**
  37. * Set the imageView `image` with an `url` and optionally a placeholder image.
  38. *
  39. * The download is asynchronous and cached.
  40. *
  41. * @param url The url for the image.
  42. * @param placeholder The image to be set initially, until the image request finishes.
  43. * @param options The options to use when downloading the image. @see BU_SDWebImageOptions for the possible values.
  44. * @param context A context contains different options to perform specify changes or processes, see `BU_SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  45. * @param setImageBlock Block used for custom set image code. If not provide, use the built-in set image code (supports `UIImageView/NSImageView` and `UIButton/NSButton` currently)
  46. * @param progressBlock A block called while image is downloading
  47. * @note the progress block is executed on a background queue
  48. * @param completedBlock A block called when operation has been completed.
  49. * This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
  50. * In case of error the image parameter is nil and the third parameter may contain an NSError.
  51. *
  52. * The forth parameter is an `BU_SDImageCacheType` enum indicating if the image was retrieved from the local cache
  53. * or from the memory cache or from the network.
  54. *
  55. * The fith parameter normally is always YES. However, if you provide SDWebImageAvoidAutoSetImage with SDWebImageProgressiveLoad options to enable progressive downloading and set the image yourself. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
  56. * block is called a last time with the full image and the last parameter set to YES.
  57. *
  58. * The last parameter is the original image URL
  59. */
  60. - (void)sdBu_internalSetImageWithURL:(nullable NSURL *)url
  61. placeholderImage:(nullable UIImage *)placeholder
  62. options:(BU_SDWebImageOptions)options
  63. context:(nullable SDWebImageContext *)context
  64. setImageBlock:(nullable SDSetImageBlock)setImageBlock
  65. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  66. completed:(nullable BU_SDInternalCompletionBlock)completedBlock;
  67. /**
  68. * Cancel the current image load
  69. */
  70. - (void)sdBu_cancelCurrentImageLoad;
  71. #if SD_UIKIT || SD_MAC
  72. #pragma mark - Image Transition
  73. /**
  74. The image transition when image load finished. See `SDWebImageTransition`.
  75. If you specify nil, do not do transition. Defautls to nil.
  76. */
  77. @property (nonatomic, strong, nullable) BU_SDWebImageTransition *sdBu_imageTransition;
  78. #pragma mark - Image Indicator
  79. /**
  80. The image indicator during the image loading. If you do not need indicator, specify nil. Defaults to nil
  81. The setter will remove the old indicator view and add new indicator view to current view's subview.
  82. @note Because this is UI related, you should access only from the main queue.
  83. */
  84. @property (nonatomic, strong, nullable) id<SDWebImageIndicator> sdBu_imageIndicator;
  85. #endif
  86. @end