[ASImageNode] A ASImageNode debug feature showing pixel scaling amount

- shows a red text label overlay on bottom right hand corner of ASImageNodes with pixel scaling factor
- import AsyncDisplayKit+Debug.h and enable using [ASImageNode setEnableImageDebugOverlay]
This commit is contained in:
Hannah Troisi
2016-03-07 09:44:16 -08:00
parent 91b81d0e80
commit d64c05a68e
3 changed files with 93 additions and 2 deletions

View File

@@ -15,8 +15,10 @@
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASDisplayNodeExtras.h>
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import "ASImageNode+CGExtras.h"
#import "AsyncDisplayKit+Debug.h"
#import "ASInternalHelpers.h"
#import "ASEqualityHelpers.h"
@@ -55,6 +57,9 @@
@end
@interface ASImageNode ()
@property (nonatomic, strong) ASTextNode *debugLabelNode;
@end
@implementation ASImageNode
{
@@ -89,6 +94,12 @@
_cropRect = CGRectMake(0.5, 0.5, 0, 0);
_cropDisplayBounds = CGRectNull;
_placeholderColor = ASDisplayNodeDefaultPlaceholderColor();
if ([ASImageNode shouldShowImageDebugOverlay]) {
_debugLabelNode = [[ASTextNode alloc] init];
_debugLabelNode.layerBacked = YES;
[self addSubnode:_debugLabelNode];
}
return self;
}
@@ -109,6 +120,7 @@
{
ASDN::MutexLocker l(_imageLock);
// if a preferredFrameSize is set, call the superclass to return that instead of using the image size.
[_debugLabelNode measure:constrainedSize];
if (CGSizeEqualToSize(self.preferredFrameSize, CGSizeZero) == NO)
return [super calculateSizeThatFits:constrainedSize];
else if (_image)
@@ -152,6 +164,12 @@
contentMode:self.contentMode];
}
- (NSDictionary *)debugLabelAttributes
{
return @{ NSFontAttributeName: [UIFont systemFontOfSize:15.0],
NSForegroundColorAttributeName: [UIColor redColor] };
}
- (UIImage *)displayWithParameters:(_ASImageNodeDrawParameters *)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{
UIImage *image;
@@ -202,6 +220,20 @@
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(floorf(bounds.size.width * contentsScale), floorf(bounds.size.height * contentsScale));
if (_debugLabelNode) {
// NSLog(@"widthScale = %f, heightScale = %f", imageSizeInPixels.width / boundsSizeInPixels.width, imageSizeInPixels.height / boundsSizeInPixels.height);
CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height);
if (pixelCountRatio != 1.0) {
NSString *scaleString = [NSString stringWithFormat:@"%.2fx", pixelCountRatio];
_debugLabelNode.attributedString = [[NSAttributedString alloc] initWithString:scaleString attributes:[self debugLabelAttributes]];
_debugLabelNode.hidden = NO;
[self setNeedsLayout];
} else {
_debugLabelNode.hidden = YES;
_debugLabelNode.attributedString = nil;
}
}
BOOL contentModeSupported = contentMode == UIViewContentModeScaleAspectFill
|| contentMode == UIViewContentModeScaleAspectFit
|| contentMode == UIViewContentModeCenter;
@@ -410,9 +442,21 @@
_imageModificationBlock = imageModificationBlock;
}
#pragma mark - Debug
- (void)layout
{
[super layout];
if (_debugLabelNode) {
CGSize boundsSize = self.bounds.size;
CGSize debugLabelSize = [_debugLabelNode measure:boundsSize];
CGPoint debugLabelOrigin = CGPointMake(boundsSize.width - debugLabelSize.width,
boundsSize.height - debugLabelSize.height);
_debugLabelNode.frame = (CGRect) {debugLabelOrigin, debugLabelSize};
}
}
@end
#pragma mark - Extras
extern asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor)
{
@@ -460,4 +504,3 @@ extern asimagenode_modification_block_t ASImageNodeTintColorModificationBlock(UI
return modifiedImage;
};
}