Merge pull request #577 from pieterclaerhout/master

Added a delegate for the web image prefetched.
This commit is contained in:
Olivier Poitrey 2013-12-06 07:57:44 -08:00
commit 3380e56a7d
2 changed files with 44 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,14 @@
// 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 +103,17 @@
- (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