CNLiveWebImageManager.m
4.66 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// CNLiveWebImageManager.m
// WSWebImageDemo
//
// Created by 王帅 on 16/7/27.
// Copyright © 2016年 王帅. All rights reserved.
//
#import "CNLiveWebImageManager.h"
#import "CNLiveWebImageDownload.h"
#import "CNLiveImageCache.h"
@interface CNLiveWebImageManager ()
@property (nonatomic, strong) CNLiveImageCache *imageCache;
@property (nonatomic, strong) NSMutableSet *failedURLs;
@end
@implementation CNLiveWebImageManager
+ (id)sharedManager
{
static id manager;
static dispatch_once_t once;
dispatch_once(&once, ^{
manager = [self new];
});
return manager;
}
- (instancetype)init
{
if ((self = [super init])) {
_imageCache = [CNLiveImageCache sharedImageCache];
_failedURLs = [NSMutableSet new];
}
return self;
}
- (id<CNLiveWebImageOperation>)downloadWebImageWithURL:(NSURL *)url options:(CNLiveWebImageOptions)options progress:(CNLiveDownloadProgressBlock)progress completed:(CNLiveDownloadCompletedBlock)completed
{
if ([url isKindOfClass:NSString.class]) {
url = [NSURL URLWithString:(NSString *)url];
}
if (![url isKindOfClass:NSURL.class]) {
url = nil;
}
BOOL isFailedUrl = NO;
@synchronized (self.failedURLs) {
isFailedUrl = [self.failedURLs containsObject:url];
}
if (!url || (!(options & CNLiveWebImageRetryFailed) && isFailedUrl)) {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
completed(nil, nil, error, YES);
}
BOOL hasCacheImage = [self.imageCache queryDiskCacheForKey:url.absoluteString done:^(UIImage *image, CNLiveImageCacheType type) {
if (image) {
completed(image,UIImageJPEGRepresentation(image, 1.0), nil, YES);
}
}];;
if (hasCacheImage) {
return nil;
}
CNLiveWebImageDownloadOptions downloadOptions = 0;
if (options & CNLiveWebImageProgressiveDownload) downloadOptions |= CNLiveWebImageDownloadProgressiveDownload;
if (options & CNLiveWebImageContinueInBackground) downloadOptions |= CNLiveWebImageDownloadContinueInBackground;
id <CNLiveWebImageOperation> operation = [[CNLiveWebImageDownload sharedDownload] downloadWebImageWithURL:url options:downloadOptions progress:progress completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (error) {
BOOL shouldBeFailedURLAlliOSVersion = (error.code != NSURLErrorNotConnectedToInternet && error.code != NSURLErrorCancelled && error.code != NSURLErrorTimedOut);
BOOL shouldBeFailedURLiOS7 = (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1 && error.code != NSURLErrorInternationalRoamingOff && error.code != NSURLErrorCallIsActive && error.code != NSURLErrorDataNotAllowed);
if (shouldBeFailedURLAlliOSVersion || shouldBeFailedURLiOS7) {
@synchronized (self.failedURLs) {
[self.failedURLs addObject:url];
}
}
}
else {
if ((options & CNLiveWebImageRetryFailed)) {
@synchronized (self.failedURLs) {
[self.failedURLs removeObject:url];
}
}
BOOL cacheOnDisk = !(options & CNLiveWebImageCacheMemoryOnly);
if (image && finished) {
if (cacheOnDisk) {
[self.imageCache storeImage:image forKey:[url absoluteString] toDisk:YES];
}
else {
[self.imageCache storeImage:image forKey:[url absoluteString] toDisk:NO];
}
}
completed(image, data, error, finished);
}
}];
return operation;
}
- (void)storeImage:(UIImage *)image imageUrl:(NSURL *)url toDisk:(BOOL)toDisk
{
[self.imageCache storeImage:image forKey:url.absoluteString toDisk:toDisk];
}
- (UIImage *)imageFromMemoryCacheWithImageUrl:(NSURL *)url
{
return [self.imageCache imageFromMemoryCacheForKey:url.absoluteString];
}
- (UIImage *)imageFromDiskCacheWithImageUrl:(NSURL *)url
{
return [self.imageCache imageFromDiskCacheForKey:url.absoluteString];
}
- (BOOL)queryDiskCacheWithImageUrl:(NSURL *)url done:(CNLiveWebImageQueryCompletedBlock)doneBlock
{
return [self.imageCache queryDiskCacheForKey:url.absoluteString done:doneBlock];
}
- (void)removeCacheWithUrl:(NSURL *)url done:(CNLiveCompletedBlock)completed
{
[self.imageCache removeCacheForKey:url.absoluteString done:completed];
}
- (long long)diskCacheSize
{
return [self.imageCache diskCacheSize];
}
- (void)clearMemory
{
[self.imageCache clearMemory];
}
- (void)clearDisk
{
[self.imageCache clearDisk];
}
@end