Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 292c1d0

Browse files
maickigarrettmoon
authored andcommitted
Further logic and documentation improvements
1 parent a399ef4 commit 292c1d0

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

AsyncDisplayKit/ASNetworkImageNode.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
2626
@interface ASNetworkImageNode : ASImageNode
2727

2828
/**
29-
* The designated initializer. Cache and Downloader are WEAK references.
29+
* The designated initializer. Cache and Downloader are WEAK references.
3030
*
3131
* @param cache The object that implements a cache of images for the image node. Weak reference.
3232
* @param downloader The object that implements image downloading for the image node. Must not be nil. Weak reference.
@@ -38,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
3838
- (instancetype)initWithCache:(nullable id<ASImageCacheProtocol>)cache downloader:(id<ASImageDownloaderProtocol>)downloader NS_DESIGNATED_INITIALIZER;
3939

4040
/**
41-
* Convenience initialiser.
41+
* Convenience initializer.
4242
*
4343
* @return An ASNetworkImageNode configured to use the NSURLSession-powered ASBasicImageDownloader, and no extra cache.
4444
*/
@@ -49,6 +49,17 @@ NS_ASSUME_NONNULL_BEGIN
4949
*/
5050
@property (nullable, nonatomic, weak, readwrite) id<ASNetworkImageNodeDelegate> delegate;
5151

52+
/**
53+
* The image to display.
54+
*
55+
* @discussion By setting an image to the image property the ASNetworkImageNode will act like a plain ASImageNode.
56+
* As soon as the URL is set the ASNetworkImageNode will act like an ASNetworkImageNode and the image property
57+
* will be managed internally. This means the image property will be cleared out and replaced by the placeholder
58+
* (<defaultImage>) image while loading and the final image after the new image data was downloaded and processed.
59+
* If you want to use a placholder image functionality use the defaultImage property instead.
60+
*/
61+
@property (nullable, nonatomic, strong) UIImage *image;
62+
5263
/**
5364
* A placeholder image to display while the URL is loading.
5465
*/
@@ -57,16 +68,21 @@ NS_ASSUME_NONNULL_BEGIN
5768
/**
5869
* The URL of a new image to download and display.
5970
*
60-
* @discussion Changing this property will reset the displayed image to a placeholder (<defaultImage>) while loading.
71+
* @discussion By setting an URL, the image property of this node will be managed internally. This means previously
72+
* directly set images to the image property will be cleared out and replaced by the placeholder (<defaultImage>) image
73+
* while loading and the final image after the new image data was downloaded and processed.
6174
*/
6275
@property (nullable, nonatomic, strong, readwrite) NSURL *URL;
6376

6477
/**
6578
* Download and display a new image.
6679
*
6780
* @param URL The URL of a new image to download and display.
68-
*
6981
* @param reset Whether to display a placeholder (<defaultImage>) while loading the new image.
82+
*
83+
* @discussion By setting an URL, the image property of this node will be managed internally. This means previously
84+
* directly set images to the image property will be cleared out and replaced by the placeholder (<defaultImage>) image
85+
* while loading and the final image after the new image data was downloaded and processed.
7086
*/
7187
- (void)setURL:(nullable NSURL *)URL resetToDefault:(BOOL)reset;
7288

AsyncDisplayKit/ASNetworkImageNode.mm

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ @interface ASNetworkImageNode ()
7373

7474
@implementation ASNetworkImageNode
7575

76+
@dynamic image;
77+
7678
- (instancetype)initWithCache:(id<ASImageCacheProtocol>)cache downloader:(id<ASImageDownloaderProtocol>)downloader
7779
{
7880
if (!(self = [super init]))
@@ -121,20 +123,13 @@ - (void)dealloc
121123
/// Setter for public image property. It has the side effect to set an internal _imageWasSetExternally that prevents setting an image internally. Setting an image internally should happen with the _setImage: method
122124
- (void)setImage:(UIImage *)image
123125
{
124-
__instanceLock__.lock();
125-
126-
#ifdef DEBUG
127-
if (_URL != nil) {
128-
NSLog(@"Setting the image directly on an %@ and setting and setting an URL is not supported. If you want to use a placeholder image please use defaultImage .", NSStringFromClass([self class]));
129-
}
130-
#endif
126+
ASDN::MutexLocker l(__instanceLock__);
131127

132128
_imageWasSetExternally = (image != nil);
133129
if (_imageWasSetExternally) {
134-
[self __cancelDownloadAndClearImage];
130+
[self _cancelDownloadAndClearImage];
135131
_URL = nil;
136132
}
137-
__instanceLock__.unlock();
138133

139134
[self _setImage:image];
140135
}
@@ -153,11 +148,6 @@ - (void)setURL:(NSURL *)URL resetToDefault:(BOOL)reset
153148
{
154149
ASDN::MutexLocker l(__instanceLock__);
155150

156-
#ifdef DEBUG
157-
if (_imageWasSetExternally) {
158-
NSLog(@"Setting the image directly on an %@ and setting and setting an URL is not supported. If you want to use a placeholder image please use defaultImage .", NSStringFromClass([self class]));
159-
}
160-
#endif
161151
_imageWasSetExternally = NO;
162152

163153
if (ASObjectIsEqual(URL, _URL)) {
@@ -352,7 +342,7 @@ - (void)didExitPreloadState
352342
return;
353343
}
354344

355-
[self __cancelDownloadAndClearImage];
345+
[self _cancelDownloadAndClearImage];
356346
}
357347
}
358348

@@ -419,7 +409,7 @@ - (void)_updateProgressImageBlockOnDownloaderIfNeeded
419409
_downloadIdentifierForProgressBlock = newDownloadIDForProgressBlock;
420410
}
421411

422-
- (void)__cancelDownloadAndClearImage
412+
- (void)_cancelDownloadAndClearImage
423413
{
424414
[self _cancelImageDownload];
425415
[self _clearImage];
@@ -428,6 +418,20 @@ - (void)__cancelDownloadAndClearImage
428418
}
429419
}
430420

421+
- (void)_cancelImageDownload
422+
{
423+
if (!_downloadIdentifier) {
424+
return;
425+
}
426+
427+
if (_downloadIdentifier) {
428+
[_downloader cancelImageDownloadForIdentifier:_downloadIdentifier];
429+
}
430+
_downloadIdentifier = nil;
431+
432+
_cacheUUID = nil;
433+
}
434+
431435
- (void)_clearImage
432436
{
433437
// Destruction of bigger images on the main thread can be expensive
@@ -449,20 +453,6 @@ - (void)_clearImage
449453
});
450454
}
451455

452-
- (void)_cancelImageDownload
453-
{
454-
if (!_downloadIdentifier) {
455-
return;
456-
}
457-
458-
if (_downloadIdentifier) {
459-
[_downloader cancelImageDownloadForIdentifier:_downloadIdentifier];
460-
}
461-
_downloadIdentifier = nil;
462-
463-
_cacheUUID = nil;
464-
}
465-
466456
- (void)_downloadImageWithCompletion:(void (^)(id <ASImageContainerProtocol> imageContainer, NSError*, id downloadIdentifier))finished
467457
{
468458
ASPerformBlockOnBackgroundThread(^{

0 commit comments

Comments
 (0)