SDWebImagePrefetcher.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "SDWebImageManager.h"
  10. @class SDWebImagePrefetcher;
  11. /**
  12. A token represents a list of URLs, can be used to cancel the download.
  13. */
  14. @interface SDWebImagePrefetchToken : NSObject <SDWebImageOperation>
  15. /**
  16. * Cancel the current prefetching.
  17. */
  18. - (void)cancel;
  19. /**
  20. list of URLs of current prefetching.
  21. */
  22. @property (nonatomic, copy, readonly, nullable) NSArray<NSURL *> *urls;
  23. @end
  24. /**
  25. The prefetcher delegate protocol
  26. */
  27. @protocol SDWebImagePrefetcherDelegate <NSObject>
  28. @optional
  29. /**
  30. * Called when an image was prefetched. Which means it's called when one URL from any of prefetching finished.
  31. *
  32. * @param imagePrefetcher The current image prefetcher
  33. * @param imageURL The image url that was prefetched
  34. * @param finishedCount The total number of images that were prefetched (successful or not)
  35. * @param totalCount The total number of images that were to be prefetched
  36. */
  37. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didPrefetchURL:(nullable NSURL *)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount;
  38. /**
  39. * Called when all images are prefetched. Which means it's called when all URLs from all of prefetching finished.
  40. * @param imagePrefetcher The current image prefetcher
  41. * @param totalCount The total number of images that were prefetched (whether successful or not)
  42. * @param skippedCount The total number of images that were skipped
  43. */
  44. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount;
  45. @end
  46. typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls);
  47. typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls);
  48. /**
  49. * Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
  50. */
  51. @interface SDWebImagePrefetcher : NSObject
  52. /**
  53. * The web image manager used by prefetcher to prefetch images.
  54. * @note You can specify a standalone manager and downloader with custom configuration suitable for image prefetching. Such as `currentDownloadCount` or `downloadTimeout`.
  55. */
  56. @property (strong, nonatomic, readonly, nonnull) SDWebImageManager *manager;
  57. /**
  58. * Maximum number of URLs to prefetch at the same time. Defaults to 3.
  59. */
  60. @property (nonatomic, assign) NSUInteger maxConcurrentPrefetchCount;
  61. /**
  62. * The options for prefetcher. Defaults to SDWebImageLowPriority.
  63. */
  64. @property (nonatomic, assign) SDWebImageOptions options;
  65. /**
  66. * The context for prefetcher. Defaults to nil.
  67. */
  68. @property (nonatomic, copy, nullable) SDWebImageContext *context;
  69. /**
  70. * Queue options for prefetcher when call the progressBlock, completionBlock and delegate methods. Defaults to Main Queue.
  71. * @note The call is asynchronously to avoid blocking target queue.
  72. * @note The delegate queue should be set before any prefetching start and may not be changed during prefetching to avoid thread-safe problem.
  73. */
  74. @property (strong, nonatomic, nonnull) dispatch_queue_t delegateQueue;
  75. /**
  76. * The delegate for the prefetcher. Defaults to nil.
  77. */
  78. @property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate;
  79. /**
  80. * Returns the global shared image prefetcher instance. It use a standalone manager which is different from shared manager.
  81. */
  82. @property (nonatomic, class, readonly, nonnull) SDWebImagePrefetcher *sharedImagePrefetcher;
  83. /**
  84. * Allows you to instantiate a prefetcher with any arbitrary image manager.
  85. */
  86. - (nonnull instancetype)initWithImageManager:(nonnull SDWebImageManager *)manager NS_DESIGNATED_INITIALIZER;
  87. /**
  88. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching. It based on the image manager so the image may from the cache and network according to the `options` property.
  89. * Prefetching is separate to each other, which means the progressBlock and completionBlock you provide is bind to the prefetching for the list of urls.
  90. * Attention that call this will not cancel previous fetched urls. You should keep the token return by this to cancel or cancel all the prefetch.
  91. *
  92. * @param urls list of URLs to prefetch
  93. * @return the token to cancel the current prefetching.
  94. */
  95. - (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls;
  96. /**
  97. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching. It based on the image manager so the image may from the cache and network according to the `options` property.
  98. * Prefetching is separate to each other, which means the progressBlock and completionBlock you provide is bind to the prefetching for the list of urls.
  99. * Attention that call this will not cancel previous fetched urls. You should keep the token return by this to cancel or cancel all the prefetch.
  100. *
  101. * @param urls list of URLs to prefetch
  102. * @param progressBlock block to be called when progress updates;
  103. * first parameter is the number of completed (successful or not) requests,
  104. * second parameter is the total number of images originally requested to be prefetched
  105. * @param completionBlock block to be called when the current prefetching is completed
  106. * first param is the number of completed (successful or not) requests,
  107. * second parameter is the number of skipped requests
  108. * @return the token to cancel the current prefetching.
  109. */
  110. - (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls
  111. progress:(nullable SDWebImagePrefetcherProgressBlock)progressBlock
  112. completed:(nullable SDWebImagePrefetcherCompletionBlock)completionBlock;
  113. /**
  114. * Remove and cancel all the prefeching for the prefetcher.
  115. */
  116. - (void)cancelPrefetching;
  117. @end