SDWebImagePrefetcher.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @protocol SDWebImagePrefetcherDelegate <NSObject>
  12. @optional
  13. /**
  14. * Called when an image was prefetched.
  15. *
  16. * @param imagePrefetcher The current image prefetcher
  17. * @param imageURL The image url that was prefetched
  18. * @param finishedCount The total number of images that were prefetched (successful or not)
  19. * @param totalCount The total number of images that were to be prefetched
  20. */
  21. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didPrefetchURL:(nullable NSURL *)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount;
  22. /**
  23. * Called when all images are prefetched.
  24. * @param imagePrefetcher The current image prefetcher
  25. * @param totalCount The total number of images that were prefetched (whether successful or not)
  26. * @param skippedCount The total number of images that were skipped
  27. */
  28. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount;
  29. @end
  30. typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls);
  31. typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls);
  32. /**
  33. * Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
  34. */
  35. @interface SDWebImagePrefetcher : NSObject
  36. /**
  37. * The web image manager
  38. */
  39. @property (strong, nonatomic, readonly, nonnull) SDWebImageManager *manager;
  40. /**
  41. * Maximum number of URLs to prefetch at the same time. Defaults to 3.
  42. */
  43. @property (nonatomic, assign) NSUInteger maxConcurrentDownloads;
  44. /**
  45. * SDWebImageOptions for prefetcher. Defaults to SDWebImageLowPriority.
  46. */
  47. @property (nonatomic, assign) SDWebImageOptions options;
  48. /**
  49. * Queue options for Prefetcher. Defaults to Main Queue.
  50. */
  51. @property (strong, nonatomic, nonnull) dispatch_queue_t prefetcherQueue;
  52. @property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate;
  53. /**
  54. * Return the global image prefetcher instance.
  55. */
  56. + (nonnull instancetype)sharedImagePrefetcher;
  57. /**
  58. * Allows you to instantiate a prefetcher with any arbitrary image manager.
  59. */
  60. - (nonnull instancetype)initWithImageManager:(nonnull SDWebImageManager *)manager NS_DESIGNATED_INITIALIZER;
  61. /**
  62. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
  63. * currently one image is downloaded at a time,
  64. * and skips images for failed downloads and proceed to the next image in the list.
  65. * Any previously-running prefetch operations are canceled.
  66. *
  67. * @param urls list of URLs to prefetch
  68. */
  69. - (void)prefetchURLs:(nullable NSArray<NSURL *> *)urls;
  70. /**
  71. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
  72. * currently one image is downloaded at a time,
  73. * and skips images for failed downloads and proceed to the next image in the list.
  74. * Any previously-running prefetch operations are canceled.
  75. *
  76. * @param urls list of URLs to prefetch
  77. * @param progressBlock block to be called when progress updates;
  78. * first parameter is the number of completed (successful or not) requests,
  79. * second parameter is the total number of images originally requested to be prefetched
  80. * @param completionBlock block to be called when prefetching is completed
  81. * first param is the number of completed (successful or not) requests,
  82. * second parameter is the number of skipped requests
  83. */
  84. - (void)prefetchURLs:(nullable NSArray<NSURL *> *)urls
  85. progress:(nullable SDWebImagePrefetcherProgressBlock)progressBlock
  86. completed:(nullable SDWebImagePrefetcherCompletionBlock)completionBlock;
  87. /**
  88. * Remove and cancel queued list
  89. */
  90. - (void)cancelPrefetching;
  91. @end