FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
AsyncDisplayKit/AsyncDisplayKit/ASNetworkImageNode.mm at master · willardLin/AsyncDisplayKit · GitHub
willardLin
/
AsyncDisplayKit
Public
forked from
facebookarchive/AsyncDisplayKit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
AsyncDisplayKit
/
AsyncDisplayKit
/
ASNetworkImageNode.mm
Copy path
More file actions
More file actions
Latest commit
History
History
History
257 lines (200 loc) · 5.77 KB
Breadcrumbs
AsyncDisplayKit
/
AsyncDisplayKit
/
ASNetworkImageNode.mm
Copy path
File metadata and controls
257 lines (200 loc) · 5.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#
import
"
ASNetworkImageNode.h
"
#
import
<
AsyncDisplayKit/ASDisplayNode+Subclasses.h
>
#
import
<
AsyncDisplayKit/ASThread.h
>
@interface
ASNetworkImageNode
()
{
ASDN
::RecursiveMutex _lock;
id
<ASImageCacheProtocol> _cache;
id
<ASImageDownloaderProtocol> _downloader;
//
Only access any of these with _lock.
id
<ASNetworkImageNodeDelegate> _delegate;
NSURL
*_URL;
UIImage *_defaultImage;
NSUUID
*_cacheUUID;
id
_imageDownload;
BOOL
_imageLoaded;
}
@end
@implementation
ASNetworkImageNode
- (
instancetype
)
initWithCache
:
(
id
<ASImageCacheProtocol>)
cache
downloader
:
(
id
<ASImageDownloaderProtocol>)
downloader
{
if
(!(self = [
super
init
]))
return
nil
;
_cache = cache;
_downloader = downloader;
_shouldCacheImage =
YES
;
return
self;
}
- (
instancetype
)
init
{
ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER
();
}
- (
void
)
dealloc
{
[
self
_cancelImageDownload
];
}
#
pragma mark
- Public methods -- must lock
- (
void
)
setURL
:
(
NSURL
*)
URL
{
[
self
setURL:
URL
resetToDefault:
YES
];
}
- (
void
)
setURL
:
(
NSURL
*)
URL
resetToDefault
:
(
BOOL
)
reset
{
ASDN
::MutexLocker
l
(_lock);
if
(
URL
== _URL || [
URL
isEqual:
_URL]) {
return
;
}
[
self
_cancelImageDownload
];
_imageLoaded =
NO
;
_URL =
URL
;
if
(reset || _URL ==
nil
)
self.
image
= _defaultImage;
if
(self.
nodeLoaded
&& self.
layer
.
superlayer
)
[
self
_lazilyLoadImageIfNecessary
];
}
- (
NSURL
*)
URL
{
ASDN
::MutexLocker
l
(_lock);
return
_URL;
}
- (
void
)
setDefaultImage
:
(UIImage *)
defaultImage
{
ASDN
::MutexLocker
l
(_lock);
if
(defaultImage == _defaultImage || [defaultImage
isEqual:
_defaultImage]) {
return
;
}
_defaultImage = defaultImage;
if
(!_imageLoaded) {
self.
image
= _defaultImage;
}
}
- (UIImage *)
defaultImage
{
ASDN
::MutexLocker
l
(_lock);
return
_defaultImage;
}
- (
void
)
setDelegate
:
(
id
<ASNetworkImageNodeDelegate>)
delegate
{
ASDN
::MutexLocker
l
(_lock);
_delegate = delegate;
}
- (
id
<ASNetworkImageNodeDelegate>)
delegate
{
ASDN
::MutexLocker
l
(_lock);
return
_delegate;
}
- (
void
)
didExitHierarchy
{
[
super
didExitHierarchy
];
{
ASDN
::MutexLocker
l
(_lock);
[
self
_cancelImageDownload
];
self.
image
= _defaultImage;
_imageLoaded =
NO
;
}
}
- (
void
)
willEnterHierarchy
{
[
super
willEnterHierarchy
];
{
ASDN
::MutexLocker
l
(_lock);
[
self
_lazilyLoadImageIfNecessary
];
}
}
#
pragma mark
- Private methods -- only call with lock.
- (
void
)
_cancelImageDownload
{
if
(!_imageDownload) {
return
;
}
[_downloader
cancelImageDownloadForIdentifier:
_imageDownload];
_imageDownload =
nil
;
_cacheUUID =
nil
;
}
- (
void
)
_downloadImageWithCompletion
:
(
void
(^)(CGImageRef))
finished
{
_imageDownload = [_downloader
downloadImageWithURL:
_URL
callbackQueue:
dispatch_get_main_queue
()
downloadProgressBlock:
NULL
completion:
^(CGImageRef responseImage,
NSError
*error) {
if
(finished !=
NULL
) {
finished
(responseImage);
}
}];
}
- (
void
)
_lazilyLoadImageIfNecessary
{
if
(!_imageLoaded && _URL !=
nil
&& _imageDownload ==
nil
) {
if
(_URL.
isFileURL
) {
{
ASDN
::MutexLocker
l
(_lock);
dispatch_async
(
dispatch_get_main_queue
(), ^{
_imageLoaded =
YES
;
if
(self.
shouldCacheImage
) {
self.
image
= [UIImage
imageNamed:
_URL.path];
}
else
{
self.
image
= [UIImage
imageWithContentsOfFile:
_URL.path];
}
[_delegate
imageNode:
self
didLoadImage:
self
.image];
});
}
}
else
{
__weak
__typeof__
(self) weakSelf = self;
void
(^finished)(CGImageRef) = ^(CGImageRef responseImage) {
__typeof__
(self) strongSelf = weakSelf;
if
(strongSelf ==
nil
) {
return
;
}
{
ASDN
::MutexLocker
l
(strongSelf->
_lock
);
if
(responseImage !=
NULL
) {
strongSelf->
_imageLoaded
=
YES
;
strongSelf.
image
= [UIImage
imageWithCGImage:
responseImage];
}
strongSelf->
_imageDownload
=
nil
;
strongSelf->
_cacheUUID
=
nil
;
}
if
(responseImage !=
NULL
) {
[strongSelf->_delegate
imageNode:
self
didLoadImage:
strongSelf.image];
}
};
if
(_cache !=
nil
) {
NSUUID
*cacheUUID = [
NSUUID
UUID
];
_cacheUUID = cacheUUID;
void
(^cacheCompletion)(CGImageRef) = ^(CGImageRef image) {
//
If the cache UUID changed, that means this request was cancelled.
if
(![_cacheUUID
isEqual:
cacheUUID]) {
return
;
}
if
(image ==
NULL
&& _downloader !=
nil
) {
[
self
_downloadImageWithCompletion:
finished];
}
else
{
finished
(image);
}
};
[_cache
fetchCachedImageWithURL:
_URL
callbackQueue:
dispatch_get_main_queue
()
completion:
cacheCompletion];
}
else
{
[
self
_downloadImageWithCompletion:
finished];
}
}
}
}
#
pragma mark
- ASDisplayNode+Subclasses
- (
void
)
asyncdisplaykit_asyncTransactionContainerStateDidChange
{
if
(self.
asyncdisplaykit_asyncTransactionContainerState
== ASAsyncTransactionContainerStateNoTransactions) {
if
(self.
layer
.
contents
!=
nil
&& [
self
.delegate
respondsToSelector:
@selector
(
imageNodeDidFinishDecoding:
)]) {
[
self
.delegate
imageNodeDidFinishDecoding:
self
];
}
}
}
@end
Back
|
FazBrowse Home
|
New Git URL