CNLiveWebImageDownloadOperation.m
5.46 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// CNLiveWebImageDownloadOperation.m
// WSWebImageDemo
//
// Created by 王帅 on 16/7/25.
// Copyright © 2016年 王帅. All rights reserved.
//
#import "CNLiveWebImageDownloadOperation.h"
#import "CNLiveWebImageOperation.h"
#import <ImageIO/ImageIO.h>
#import "UIImage+MultiFormat.h"
#define CNLiveWebImageBackgroundSessionIdentifier @"CNLiveWebImageBackgroundSessionIdentifier"
@interface CNLiveWebImageDownloadOperation ()<CNLiveWebImageOperation, NSURLSessionDataDelegate, NSURLSessionTaskDelegate, NSURLSessionDelegate>
@property (nonatomic, copy) CNLiveDownloadProgressBlock progressBlock;
@property (nonatomic, copy) CNLiveDownloadCompletedBlock completedBlock;
@property (nonatomic, assign) CNLiveWebImageDownloadOptions options;
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSURLSessionConfiguration *config;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDataTask *dataTask;
@property (nonatomic, assign) NSInteger expectedSize;
@property (nonatomic, strong) NSMutableData *imageData;
@property (nonatomic, strong) NSURLResponse *response;
@end
@implementation CNLiveWebImageDownloadOperation
- (id)initWithURL:(NSURL *)url options:(CNLiveWebImageDownloadOptions)options progress:(CNLiveDownloadProgressBlock)progress completed:(CNLiveDownloadCompletedBlock)completed
{
if ((self = [super init])) {
_url = url;
_progressBlock = progress;
_completedBlock = completed;
_options = options;
}
return self;
}
- (void)start
{
@synchronized (self) {
if (self.isCancelled) {
return;
}
}
self.dataTask = [self.session dataTaskWithURL:self.url];
[self.dataTask resume];
[self.session finishTasksAndInvalidate];
}
- (NSURLSessionConfiguration *)config
{
if (!_config) {
if (self.options & CNLiveWebImageDownloadContinueInBackground) {
_config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:CNLiveWebImageBackgroundSessionIdentifier];
}
else {
_config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
}
}
return _config;
}
- (NSURLSession *)session
{
if (!_session) {
_session = [NSURLSession sessionWithConfiguration:self.config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
return _session;
}
#pragma mark <NSURLSessionDataDelegate>
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
if (![response respondsToSelector:@selector(statusCode)] || ([((NSHTTPURLResponse *)response) statusCode] < 400 && [((NSHTTPURLResponse *)response) statusCode] != 304)) {
NSInteger expected = response.expectedContentLength > 0 ? (NSInteger)response.expectedContentLength : 0;
self.expectedSize = expected;
if (self.progressBlock) {
self.progressBlock(0, expected);
}
self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
self.response = response;
completionHandler(NSURLSessionResponseAllow);
}
else {
[self.dataTask cancel];
dispatch_async(dispatch_get_main_queue(), ^{
if (self.completedBlock) {
self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
}
});
}
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.imageData appendData:data];
if (self.options & CNLiveWebImageDownloadProgressiveDownload && self.completedBlock) {
const NSInteger totalSize = self.imageData.length;
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)self.imageData, NULL);
if (totalSize < self.expectedSize) {
CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
if (partialImageRef) {
UIImage *image = [UIImage imageWithCGImage:partialImageRef];
CGImageRelease(partialImageRef);
dispatch_async(dispatch_get_main_queue(), ^{
if (self.completedBlock) {
self.completedBlock(image, self.imageData, nil, NO);
}
});
}
}
CFRelease(imageSource);
}
dispatch_async(dispatch_get_main_queue(), ^{
if (self.progressBlock) {
self.progressBlock(self.imageData.length, self.expectedSize);
}
});
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.completedBlock) {
self.completedBlock(nil, nil, error, NO);
}
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.completedBlock) {
self.completedBlock([UIImage ws_imageWithData:self.imageData], self.imageData, nil, YES);
}
});
}
}
- (void)cancel
{
if (self.session) {
[self.session invalidateAndCancel];
}
self.completedBlock = nil;
self.progressBlock = nil;
self.session = nil;
self.imageData = nil;
}
@end