SDWebImagePrefetcher.m
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "SDWebImagePrefetcher.h"
#import "SDWebImageManager.h"
@interface SDWebImagePrefetcher ()
@property (nonatomic, retain) NSArray *prefetchURLs;
@end
@implementation SDWebImagePrefetcher
static SDWebImagePrefetcher *instance;
@synthesize prefetchURLs;
@synthesize maxConcurrentDownloads;
@synthesize options;
+ (SDWebImagePrefetcher *)sharedImagePrefetcher
{
if (instance == nil)
{
instance = [[SDWebImagePrefetcher alloc] init];
instance.maxConcurrentDownloads = 3;
instance.options = (SDWebImageLowPriority);
}
return instance;
}
- (void)startPrefetchingAtIndex:(NSUInteger)index withManager:(SDWebImageManager *)imageManager
{
if (index >= [self.prefetchURLs count]) return;
_requestedCount++;
[imageManager downloadWithURL:[self.prefetchURLs objectAtIndex:index] delegate:self options:self.options];
}
- (void)reportStatus
{
NSUInteger total = [self.prefetchURLs count];
NSLog(@"Finished prefetching (%d successful, %d skipped, timeElasped %.2f)", total - _skippedCount, _skippedCount, CFAbsoluteTimeGetCurrent() - _startedTime);
}
- (void)prefetchURLs:(NSArray *)urls
{
[self cancelPrefetching]; // Prevent duplicate prefetch request
_startedTime = CFAbsoluteTimeGetCurrent();
self.prefetchURLs = urls;
// Starts prefetching from the very first image on the list with the max allowed concurrency
NSUInteger listCount = [self.prefetchURLs count];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
for (NSUInteger i = 0; i < self.maxConcurrentDownloads && _requestedCount < listCount; i++)
{
[self startPrefetchingAtIndex:i withManager:manager];
}
}
- (void)cancelPrefetching
{
self.prefetchURLs = nil;
_skippedCount = 0;
_requestedCount = 0;
_finishedCount = 0;
[[SDWebImageManager sharedManager] cancelForDelegate:self];
}
#pragma mark SDWebImagePrefetcher (SDWebImageManagerDelegate)
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
_finishedCount++;
NSLog(@"Prefetched %d out of %d", _finishedCount, [self.prefetchURLs count]);
if ([self.prefetchURLs count] > _requestedCount)
{
[self startPrefetchingAtIndex:_requestedCount withManager:imageManager];
}
else if (_finishedCount == _requestedCount)
{
[self reportStatus];
}
}
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error
{
_finishedCount++;
NSLog(@"Prefetched %d out of %d (Failed)", _finishedCount, [self.prefetchURLs count]);
// Add last failed
_skippedCount++;
if ([self.prefetchURLs count] > _requestedCount)
{
[self startPrefetchingAtIndex:_requestedCount withManager:imageManager];
}
else if (_finishedCount == _requestedCount)
{
[self reportStatus];
}
}
- (void)dealloc
{
self.prefetchURLs = nil;
SDWISuperDealoc;
}
@end