Added a delegate for the web image prefetched.

This commit is contained in:
Pieter Claerhout 2013-12-06 16:21:00 +01:00
parent 6878fba926
commit 1a917d8239
2 changed files with 42 additions and 1 deletions

View File

@ -9,6 +9,33 @@
#import <Foundation/Foundation.h>
#import "SDWebImageManager.h"
@class SDWebImagePrefetcher;
@protocol SDWebImagePrefetcherDelegate <NSObject>
@optional
/**
* Called when an image was prefetched.
*
* @param imagePrefetcher The current image prefetcher
* @param imageURL The image url that was prefetched
* @param finishedCount The total number of images that were prefetched
* @param totalCount The total number of images that need to be prefetched
*/
- (void)imagePrefetcher:(SDWebImagePrefetcher*)imagePrefetcher didPrefetchURL:(NSURL*)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount;
/**
* Called when all images are prefetched.
* @param imagePrefetcher The current image prefetcher
* @param totalCount The total number of images that need to be prefetched
* @param skippedCount The total number of images that were skipped
*/
- (void)imagePrefetcher:(SDWebImagePrefetcher*)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount;
@end
/**
* Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
*/
@ -24,6 +51,7 @@
*/
@property (nonatomic, assign) SDWebImageOptions options;
@property (weak, nonatomic) id<SDWebImagePrefetcherDelegate> delegate;
/**
* Return the global image prefetcher instance.

View File

@ -76,6 +76,13 @@
// Add last failed
self.skippedCount++;
}
if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didPrefetchURL:finishedCount:totalCount:)]) {
[self.delegate imagePrefetcher:self
didPrefetchURL:self.prefetchURLs[index]
finishedCount:self.finishedCount
totalCount:self.prefetchURLs.count
];
}
if (self.prefetchURLs.count > self.requestedCount)
{
@ -95,10 +102,16 @@
- (void)reportStatus
{
#ifdef SD_VERBOSE
NSUInteger total = [self.prefetchURLs count];
#ifdef SD_VERBOSE
NSLog(@"Finished prefetching (%d successful, %d skipped, timeElasped %.2f)", total - self.skippedCount, self.skippedCount, CFAbsoluteTimeGetCurrent() - self.startedTime);
#endif
if ([self.delegate respondsToSelector:@selector(imagePrefetcher:didFinishWithTotalCount:skippedCount:)]) {
[self.delegate imagePrefetcher:self
didFinishWithTotalCount:(total - self.skippedCount)
skippedCount:self.skippedCount
];
}
}
- (void)prefetchURLs:(NSArray *)urls