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;
/**
* @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 */

View File

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

View File

@ -111,7 +111,9 @@
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
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;
else
return CGSizeZero;

View File

@ -51,11 +51,6 @@
*/
@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.
*

View File

@ -18,17 +18,16 @@
ASDN::RecursiveMutex _lock;
id<ASImageCacheProtocol> _cache;
id<ASImageDownloaderProtocol> _downloader;
// Only access any of these with _lock.
__weak id<ASNetworkImageNodeDelegate> _delegate;
NSURL *_URL;
UIImage *_defaultImage;
CGSize _expectedImageSize;
NSUUID *_cacheUUID;
id _imageDownload;
BOOL _imageLoaded;
}
@ -41,12 +40,11 @@
{
if (!(self = [super init]))
return nil;
_cache = cache;
_downloader = downloader;
_shouldCacheImage = YES;
_expectedImageSize = CGSizeZero;
return self;
}
@ -70,19 +68,19 @@
- (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];
}
@ -96,12 +94,12 @@
- (void)setDefaultImage:(UIImage *)defaultImage
{
ASDN::MutexLocker l(_lock);
if (defaultImage == _defaultImage || [defaultImage isEqual:_defaultImage]) {
return;
}
_defaultImage = defaultImage;
if (!_imageLoaded) {
self.image = _defaultImage;
}
@ -113,18 +111,6 @@
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
{
ASDN::MutexLocker l(_lock);
@ -140,17 +126,17 @@
- (void)displayWillStart
{
[super displayWillStart];
[self fetchData];
}
- (void)clearFetchedData
{
[super clearFetchedData];
{
ASDN::MutexLocker l(_lock);
[self _cancelImageDownload];
self.image = _defaultImage;
_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.
- (void)_cancelImageDownload
@ -183,10 +160,10 @@
if (!_imageDownload) {
return;
}
[_downloader cancelImageDownloadForIdentifier:_imageDownload];
_imageDownload = nil;
_cacheUUID = nil;
}
@ -208,16 +185,16 @@
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];
});
}
@ -228,42 +205,42 @@
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:strongSelf 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];

View File

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