BU_SDImageLoader.h 6.9 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_SDWebImageOperation.h"
  11. typedef void(^SDImageLoaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
  12. typedef void(^SDImageLoaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
  13. #pragma mark - Context Options
  14. /**
  15. A `UIImage` instance from `SDWebImageManager` when you specify `SDWebImageRefreshCached` and image cache hit.
  16. This can be a hint for image loader to load the image from network and refresh the image from remote location if needed. If the image from remote location does not change, you should call the completion with `SDWebImageErrorCacheNotModified` error. (UIImage)
  17. @note If you don't implement `SDWebImageRefreshCached` support, you do not need to care abot this context option.
  18. */
  19. FOUNDATION_EXPORT BU_SDWebImageContextOption _Nonnull const BU_SDWebImageContextLoaderCachedImage;
  20. #pragma mark - Helper method
  21. /**
  22. This is the built-in decoding process for image download from network or local file.
  23. @note If you want to implement your custom loader with `requestImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
  24. @param imageData The image data from the network. Should not be nil
  25. @param imageURL The image URL from the input. Should not be nil
  26. @param options The options arg from the input
  27. @param context The context arg from the input
  28. @return The decoded image for current image data load from the network
  29. */
  30. FOUNDATION_EXPORT UIImage * _Nullable BU_SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BU_SDWebImageOptions options, SDWebImageContext * _Nullable context);
  31. /**
  32. This is the built-in decoding process for image progressive download from network. It's used when `SDWebImageProgressiveLoad` option is set. (It's not required when your loader does not support progressive image loading)
  33. @note If you want to implement your custom loader with `requestImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
  34. @param imageData The image data from the network so far. Should not be nil
  35. @param imageURL The image URL from the input. Should not be nil
  36. @param finished Pass NO to specify the download process has not finished. Pass YES when all image data has finished.
  37. @param operation The loader operation associated with current progressive download. Why to provide this is because progressive decoding need to store the partial decoded context for each operation to avoid conflict. You should provide the operation from `loadImageWithURL:` method return value.
  38. @param options The options arg from the input
  39. @param context The context arg from the input
  40. @return The decoded progressive image for current image data load from the network
  41. */
  42. FOUNDATION_EXPORT UIImage * _Nullable BU_SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<BU_SDWebImageOperation> _Nonnull operation, BU_SDWebImageOptions options, SDWebImageContext * _Nullable context);
  43. #pragma mark - SDImageLoader
  44. /**
  45. This is the protocol to specify custom image load process. You can create your own class to conform this protocol and use as a image loader to load image from network or any avaiable remote resources defined by yourself.
  46. If you want to implement custom loader for image download from network or local file, you just need to concentrate on image data download only. After the download finish, call `BU_SDImageLoaderDecodeImageData` or `BU_SDImageLoaderDecodeProgressiveImageData` to use the built-in decoding process and produce image (Remember to call in the global queue). And finally callback the completion block.
  47. If you directlly get the image instance using some third-party SDKs, such as image directlly from Photos framework. You can process the image data and image instance by yourself without that built-in decoding process. And finally callback the completion block.
  48. @note It's your responsibility to load the image in the desired global queue(to avoid block main queue). We do not dispatch these method call in a global queue but just from the call queue (For `SDWebImageManager`, it typically call from the main queue).
  49. */
  50. @protocol BU_SDImageLoader <NSObject>
  51. /**
  52. Whether current image loader supports to load the provide image URL.
  53. This will be checked everytime a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call `requestImageWithURL:options:context:progress:completed:`.
  54. @param url The image URL to be loaded.
  55. @return YES to continue download, NO to stop download.
  56. */
  57. - (BOOL)canRequestImageForURL:(nullable NSURL *)url;
  58. /**
  59. Load the image and image data with the given URL and return the image data. You're responsible for producing the image instance.
  60. @param url The URL represent the image. Note this may not be a HTTP URL
  61. @param options A mask to specify options to use for this request
  62. @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.
  63. @param progressBlock A block called while image is downloading
  64. * @note the progress block is executed on a background queue
  65. @param completedBlock A block called when operation has been completed.
  66. @return An operation which allow the user to cancel the current request.
  67. */
  68. - (nullable id<BU_SDWebImageOperation>)requestImageWithURL:(nullable NSURL *)url
  69. options:(BU_SDWebImageOptions)options
  70. context:(nullable SDWebImageContext *)context
  71. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  72. completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
  73. /**
  74. Whether the error from image loader should be marked indded un-recoverable or not.
  75. If this return YES, failed URL which does not using `SDWebImageRetryFailed` will be blocked into black list. Else not.
  76. @param url The URL represent the image. Note this may not be a HTTP URL
  77. @param error The URL's loading error, from previous `requestImageWithURL:options:context:progress:completed:` completedBlock's error.
  78. @return Whether to block this url or not. Return YES to mark this URL as failed.
  79. */
  80. - (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url
  81. error:(nonnull NSError *)error;
  82. @end