15
15
16
16
@interface ApptentiveNetworkImageView ()
17
17
18
- @property (nullable , strong , nonatomic ) NSURLConnection *connection;
19
- @property (nullable , strong , nonatomic ) NSURLResponse *response;
20
- @property (nullable , strong , nonatomic ) NSMutableData *imageData;
18
+ @property (nullable , strong , nonatomic ) NSURLSessionDataTask *task;
21
19
22
20
@end
23
21
24
22
25
23
@implementation ApptentiveNetworkImageView
26
24
27
- - (id )initWithFrame : (CGRect )frame {
28
- self = [super initWithFrame: frame];
29
- if (self) {
30
- // Initialization code
31
- _useCache = YES ;
32
- }
33
- return self;
34
- }
35
-
36
- - (void )awakeFromNib {
37
- [super awakeFromNib ];
38
- self.useCache = YES ;
39
- }
40
-
41
25
- (void )dealloc {
42
- [_connection cancel ];
26
+ [_task cancel ];
43
27
}
44
28
45
29
- (void )restartDownload {
46
- if (self.connection ) {
47
- [self .connection cancel ];
48
- self.connection = nil ;
30
+ if (self.task ) {
31
+ [self .task cancel ];
32
+ self.task = nil ;
49
33
}
34
+
50
35
if (self.imageURL ) {
51
- NSURLRequest *request = [NSURLRequest requestWithURL: self .imageURL];
52
-
53
- NSURLCache *cache = [[Apptentive sharedConnection ].backend imageCache ];
54
- BOOL cacheHit = NO ;
55
- if (cache) {
56
- NSCachedURLResponse *cachedResponse = [cache cachedResponseForRequest: request];
57
- if (cachedResponse && self.useCache ) {
58
- UIImage *i = [UIImage imageWithData: cachedResponse.data];
59
- if (i) {
60
- self.image = i;
61
- cacheHit = YES ;
36
+ self.task = [[NSURLSession sharedSession ] dataTaskWithURL: self .imageURL completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
37
+ if (data == nil ) {
38
+ ApptentiveLogError (@" Unable to download image at %@ : %@ " , self.imageURL , error);
39
+ self.task = nil ;
40
+
41
+ if ([self .delegate respondsToSelector: @selector (networkImageView:didFailWithError: )]) {
42
+ dispatch_async (dispatch_get_main_queue (), ^{
43
+ [self .delegate networkImageView: self didFailWithError: error];
44
+ });
45
+ }
46
+ } else {
47
+ UIImage *newImage = [UIImage imageWithData: data];
48
+ if (newImage) {
49
+ dispatch_async (dispatch_get_main_queue (), ^{
50
+ self.image = newImage;
51
+ });
62
52
}
63
53
}
64
- }
54
+ }];
65
55
66
- if (!cacheHit) {
67
- self.connection = [[NSURLConnection alloc ] initWithRequest: request delegate: self startImmediately: NO ];
68
- [self .connection scheduleInRunLoop: [NSRunLoop currentRunLoop ] forMode: NSRunLoopCommonModes ];
69
- [self .connection start ];
70
- }
56
+ [self .task resume ];
71
57
}
72
58
}
73
59
@@ -78,52 +64,6 @@ - (void)setImageURL:(NSURL *)anImageURL {
78
64
}
79
65
}
80
66
81
- #pragma mark NSURLConnectionDelegate
82
- - (void )connection : (NSURLConnection *)aConnection didFailWithError : (NSError *)error {
83
- if (aConnection == self.connection ) {
84
- ApptentiveLogError (@" Unable to download image at %@ : %@ " , self.imageURL , error);
85
- self.connection = nil ;
86
-
87
- if ([self .delegate respondsToSelector: @selector (networkImageView:didFailWithError: )]) {
88
- [self .delegate networkImageView: self didFailWithError: error];
89
- }
90
- }
91
- }
92
-
93
- #pragma mark NSURLConnectionDataDelegate
94
- - (void )connection : (NSURLConnection *)aConnection didReceiveResponse : (NSURLResponse *)aResponse {
95
- if (aConnection == self.connection ) {
96
- self.imageData = [[NSMutableData alloc ] init ];
97
- self.response = [aResponse copy ];
98
- }
99
- }
100
-
101
- - (void )connection : (NSURLConnection *)aConnection didReceiveData : (NSData *)data {
102
- if (aConnection == self.connection ) {
103
- [self .imageData appendData: data];
104
- }
105
- }
106
-
107
- - (void )connectionDidFinishLoading : (NSURLConnection *)aConnection {
108
- if (self.connection == aConnection) {
109
- UIImage *newImage = [UIImage imageWithData: self .imageData];
110
- if (newImage) {
111
- self.image = newImage;
112
- if (self.useCache ) {
113
- NSURLRequest *request = [NSURLRequest requestWithURL: self .imageURL];
114
- NSURLCache *cache = [[Apptentive sharedConnection ].backend imageCache ];
115
- NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc ] initWithResponse: self .response data: self .imageData userInfo: nil storagePolicy: NSURLCacheStorageAllowed];
116
- [cache storeCachedResponse: cachedResponse forRequest: request];
117
- cachedResponse = nil ;
118
-
119
- if ([self .delegate respondsToSelector: @selector (networkImageViewDidLoad: )]) {
120
- [self .delegate networkImageViewDidLoad: self ];
121
- }
122
- }
123
- }
124
- }
125
- }
126
-
127
67
@end
128
68
129
69
NS_ASSUME_NONNULL_END
0 commit comments