Updates to expectedImageSize

* renamed property to requestedLayoutSize
* moved property to ASDisplayNode
This commit is contained in:
rcancro 2015-08-03 22:09:50 -07:00
parent eae76d26b7
commit 496cc43337
6 changed files with 57 additions and 60 deletions

View File

@ -185,6 +185,15 @@ typedef CALayer *(^ASDisplayNodeLayerBlock)();
*/ */
@property (nonatomic, readonly, assign) ASSizeRange constrainedSizeForCalculatedLayout; @property (nonatomic, readonly, assign) ASSizeRange constrainedSizeForCalculatedLayout;
/**
* @abstract Used by some implementations of measureWithSizeRange: to provide an intrisic content size
* to a node when one cannot be computed from its subnodes
*
* @return The requested size of this node
*/
@property (atomic, assign, readwrite) CGSize requestedLayoutSize;
/** @name Managing the nodes hierarchy */ /** @name Managing the nodes hierarchy */

View File

@ -47,6 +47,7 @@
@synthesize flexShrink = _flexShrink; @synthesize flexShrink = _flexShrink;
@synthesize flexBasis = _flexBasis; @synthesize flexBasis = _flexBasis;
@synthesize alignSelf = _alignSelf; @synthesize alignSelf = _alignSelf;
@synthesize requestedLayoutSize = _requestedLayoutSize;
BOOL ASDisplayNodeSubclassOverridesSelector(Class subclass, SEL selector) BOOL ASDisplayNodeSubclassOverridesSelector(Class subclass, SEL selector)
{ {
@ -135,6 +136,7 @@ void ASDisplayNodePerformBlockOnMainThread(void (^block)())
_methodOverrides = overrides; _methodOverrides = overrides;
_flexBasis = ASRelativeDimensionUnconstrained; _flexBasis = ASRelativeDimensionUnconstrained;
_requestedLayoutSize = CGSizeZero;
} }
- (id)init - (id)init
@ -1333,6 +1335,18 @@ static NSInteger incrementIfFound(NSInteger i) {
return _constrainedSize; return _constrainedSize;
} }
-(void)setRequestedLayoutSize:(CGSize)requestedLayoutSize
{
ASDN::MutexLocker l(_propertyLock);
_requestedLayoutSize = requestedLayoutSize;
}
- (CGSize)requestedLayoutSize
{
ASDN::MutexLocker l(_propertyLock);
return _requestedLayoutSize;
}
- (UIImage *)placeholderImage - (UIImage *)placeholderImage
{ {
return nil; return nil;

View File

@ -111,7 +111,9 @@
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{ {
ASDN::MutexLocker l(_imageLock); ASDN::MutexLocker l(_imageLock);
if (_image) if (CGSizeEqualToSize(self.requestedLayoutSize, CGSizeZero) == NO)
return CGSizeMake(MIN(constrainedSize.width, self.requestedLayoutSize.width), MIN(constrainedSize.height, self.requestedLayoutSize.height));
else if (_image)
return _image.size; return _image.size;
else else
return CGSizeZero; return CGSizeZero;

View File

@ -51,11 +51,6 @@
*/ */
@property (atomic, strong, readwrite) UIImage *defaultImage; @property (atomic, strong, readwrite) UIImage *defaultImage;
/**
* The expected size of the image that will download
*/
@property (atomic, assign, readwrite) CGSize expectedImageSize;
/** /**
* The URL of a new image to download and display. * The URL of a new image to download and display.
* *

View File

@ -18,17 +18,16 @@
ASDN::RecursiveMutex _lock; ASDN::RecursiveMutex _lock;
id<ASImageCacheProtocol> _cache; id<ASImageCacheProtocol> _cache;
id<ASImageDownloaderProtocol> _downloader; id<ASImageDownloaderProtocol> _downloader;
// Only access any of these with _lock. // Only access any of these with _lock.
__weak id<ASNetworkImageNodeDelegate> _delegate; __weak id<ASNetworkImageNodeDelegate> _delegate;
NSURL *_URL; NSURL *_URL;
UIImage *_defaultImage; UIImage *_defaultImage;
CGSize _expectedImageSize;
NSUUID *_cacheUUID; NSUUID *_cacheUUID;
id _imageDownload; id _imageDownload;
BOOL _imageLoaded; BOOL _imageLoaded;
} }
@ -41,12 +40,11 @@
{ {
if (!(self = [super init])) if (!(self = [super init]))
return nil; return nil;
_cache = cache; _cache = cache;
_downloader = downloader; _downloader = downloader;
_shouldCacheImage = YES; _shouldCacheImage = YES;
_expectedImageSize = CGSizeZero;
return self; return self;
} }
@ -70,19 +68,19 @@
- (void)setURL:(NSURL *)URL resetToDefault:(BOOL)reset - (void)setURL:(NSURL *)URL resetToDefault:(BOOL)reset
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
if (URL == _URL || [URL isEqual:_URL]) { if (URL == _URL || [URL isEqual:_URL]) {
return; return;
} }
[self _cancelImageDownload]; [self _cancelImageDownload];
_imageLoaded = NO; _imageLoaded = NO;
_URL = URL; _URL = URL;
if (reset || _URL == nil) if (reset || _URL == nil)
self.image = _defaultImage; self.image = _defaultImage;
if (self.nodeLoaded && self.layer.superlayer) if (self.nodeLoaded && self.layer.superlayer)
[self _lazilyLoadImageIfNecessary]; [self _lazilyLoadImageIfNecessary];
} }
@ -96,12 +94,12 @@
- (void)setDefaultImage:(UIImage *)defaultImage - (void)setDefaultImage:(UIImage *)defaultImage
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
if (defaultImage == _defaultImage || [defaultImage isEqual:_defaultImage]) { if (defaultImage == _defaultImage || [defaultImage isEqual:_defaultImage]) {
return; return;
} }
_defaultImage = defaultImage; _defaultImage = defaultImage;
if (!_imageLoaded) { if (!_imageLoaded) {
self.image = _defaultImage; self.image = _defaultImage;
} }
@ -113,18 +111,6 @@
return _defaultImage; return _defaultImage;
} }
-(void)setExpectedImageSize:(CGSize)expectedImageSize
{
ASDN::MutexLocker l(_lock);
_expectedImageSize = expectedImageSize;
}
- (CGSize)expectedImageSize
{
ASDN::MutexLocker l(_lock);
return _expectedImageSize;
}
- (void)setDelegate:(id<ASNetworkImageNodeDelegate>)delegate - (void)setDelegate:(id<ASNetworkImageNodeDelegate>)delegate
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
@ -140,17 +126,17 @@
- (void)displayWillStart - (void)displayWillStart
{ {
[super displayWillStart]; [super displayWillStart];
[self fetchData]; [self fetchData];
} }
- (void)clearFetchedData - (void)clearFetchedData
{ {
[super clearFetchedData]; [super clearFetchedData];
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
[self _cancelImageDownload]; [self _cancelImageDownload];
self.image = _defaultImage; self.image = _defaultImage;
_imageLoaded = NO; _imageLoaded = NO;
@ -167,15 +153,6 @@
} }
} }
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
ASDN::MutexLocker l(_lock);
if (CGSizeEqualToSize(CGSizeZero, _expectedImageSize))
return [super calculateSizeThatFits:constrainedSize];
else
return _expectedImageSize;
}
#pragma mark - Private methods -- only call with lock. #pragma mark - Private methods -- only call with lock.
- (void)_cancelImageDownload - (void)_cancelImageDownload
@ -183,10 +160,10 @@
if (!_imageDownload) { if (!_imageDownload) {
return; return;
} }
[_downloader cancelImageDownloadForIdentifier:_imageDownload]; [_downloader cancelImageDownloadForIdentifier:_imageDownload];
_imageDownload = nil; _imageDownload = nil;
_cacheUUID = nil; _cacheUUID = nil;
} }
@ -208,16 +185,16 @@
if (_URL.isFileURL) { if (_URL.isFileURL) {
{ {
ASDN::MutexLocker l(_lock); ASDN::MutexLocker l(_lock);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
_imageLoaded = YES; _imageLoaded = YES;
if (self.shouldCacheImage) { if (self.shouldCacheImage) {
self.image = [UIImage imageNamed:_URL.path]; self.image = [UIImage imageNamed:_URL.path];
} else { } else {
self.image = [UIImage imageWithContentsOfFile:_URL.path]; self.image = [UIImage imageWithContentsOfFile:_URL.path];
} }
[_delegate imageNode:self didLoadImage:self.image]; [_delegate imageNode:self didLoadImage:self.image];
}); });
} }
@ -228,42 +205,42 @@
if (strongSelf == nil) { if (strongSelf == nil) {
return; return;
} }
{ {
ASDN::MutexLocker l(strongSelf->_lock); ASDN::MutexLocker l(strongSelf->_lock);
if (responseImage != NULL) { if (responseImage != NULL) {
strongSelf->_imageLoaded = YES; strongSelf->_imageLoaded = YES;
strongSelf.image = [UIImage imageWithCGImage:responseImage]; strongSelf.image = [UIImage imageWithCGImage:responseImage];
} }
strongSelf->_imageDownload = nil; strongSelf->_imageDownload = nil;
strongSelf->_cacheUUID = nil; strongSelf->_cacheUUID = nil;
} }
if (responseImage != NULL) { if (responseImage != NULL) {
[strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image]; [strongSelf->_delegate imageNode:strongSelf didLoadImage:strongSelf.image];
} }
}; };
if (_cache != nil) { if (_cache != nil) {
NSUUID *cacheUUID = [NSUUID UUID]; NSUUID *cacheUUID = [NSUUID UUID];
_cacheUUID = cacheUUID; _cacheUUID = cacheUUID;
void (^cacheCompletion)(CGImageRef) = ^(CGImageRef image) { void (^cacheCompletion)(CGImageRef) = ^(CGImageRef image) {
// If the cache UUID changed, that means this request was cancelled. // If the cache UUID changed, that means this request was cancelled.
if (![_cacheUUID isEqual:cacheUUID]) { if (![_cacheUUID isEqual:cacheUUID]) {
return; return;
} }
if (image == NULL && _downloader != nil) { if (image == NULL && _downloader != nil) {
[self _downloadImageWithCompletion:finished]; [self _downloadImageWithCompletion:finished];
} else { } else {
finished(image); finished(image);
} }
}; };
[_cache fetchCachedImageWithURL:_URL [_cache fetchCachedImageWithURL:_URL
callbackQueue:dispatch_get_main_queue() callbackQueue:dispatch_get_main_queue()
completion:cacheCompletion]; completion:cacheCompletion];

View File

@ -83,7 +83,7 @@ static const CGFloat kInnerPadding = 10.0f;
_imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://placekitten.com/%zd/%zd", _imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://placekitten.com/%zd/%zd",
(NSInteger)roundl(_kittenSize.width), (NSInteger)roundl(_kittenSize.width),
(NSInteger)roundl(_kittenSize.height)]]; (NSInteger)roundl(_kittenSize.height)]];
_imageNode.expectedImageSize = CGSizeMake(kImageSize, kImageSize); _imageNode.requestedLayoutSize = CGSizeMake(kImageSize, kImageSize);
// _imageNode.contentMode = UIViewContentModeCenter; // _imageNode.contentMode = UIViewContentModeCenter;
[self addSubnode:_imageNode]; [self addSubnode:_imageNode];