[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/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASDisplayNodeExtras.h> #import <AsyncDisplayKit/ASDisplayNodeExtras.h>
#import <AsyncDisplayKit/ASDisplayNode+Beta.h> #import <AsyncDisplayKit/ASDisplayNode+Beta.h>
#import <AsyncDisplayKit/ASTextNode.h>
#import "ASImageNode+CGExtras.h" #import "ASImageNode+CGExtras.h"
#import "AsyncDisplayKit+Debug.h"
#import "ASInternalHelpers.h" #import "ASInternalHelpers.h"
#import "ASEqualityHelpers.h" #import "ASEqualityHelpers.h"
@@ -55,6 +57,9 @@
@end @end
@interface ASImageNode ()
@property (nonatomic, strong) ASTextNode *debugLabelNode;
@end
@implementation ASImageNode @implementation ASImageNode
{ {
@@ -89,6 +94,12 @@
_cropRect = CGRectMake(0.5, 0.5, 0, 0); _cropRect = CGRectMake(0.5, 0.5, 0, 0);
_cropDisplayBounds = CGRectNull; _cropDisplayBounds = CGRectNull;
_placeholderColor = ASDisplayNodeDefaultPlaceholderColor(); _placeholderColor = ASDisplayNodeDefaultPlaceholderColor();
if ([ASImageNode shouldShowImageDebugOverlay]) {
_debugLabelNode = [[ASTextNode alloc] init];
_debugLabelNode.layerBacked = YES;
[self addSubnode:_debugLabelNode];
}
return self; return self;
} }
@@ -109,6 +120,7 @@
{ {
ASDN::MutexLocker l(_imageLock); ASDN::MutexLocker l(_imageLock);
// if a preferredFrameSize is set, call the superclass to return that instead of using the image size. // 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) if (CGSizeEqualToSize(self.preferredFrameSize, CGSizeZero) == NO)
return [super calculateSizeThatFits:constrainedSize]; return [super calculateSizeThatFits:constrainedSize];
else if (_image) else if (_image)
@@ -152,6 +164,12 @@
contentMode:self.contentMode]; 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 *)displayWithParameters:(_ASImageNodeDrawParameters *)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{ {
UIImage *image; UIImage *image;
@@ -202,6 +220,20 @@
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale); CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(floorf(bounds.size.width * contentsScale), floorf(bounds.size.height * contentsScale)); 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 BOOL contentModeSupported = contentMode == UIViewContentModeScaleAspectFill
|| contentMode == UIViewContentModeScaleAspectFit || contentMode == UIViewContentModeScaleAspectFit
|| contentMode == UIViewContentModeCenter; || contentMode == UIViewContentModeCenter;
@@ -410,9 +442,21 @@
_imageModificationBlock = imageModificationBlock; _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 @end
#pragma mark - Extras #pragma mark - Extras
extern asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor) extern asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor)
{ {
@@ -460,4 +504,3 @@ extern asimagenode_modification_block_t ASImageNodeTintColorModificationBlock(UI
return modifiedImage; return modifiedImage;
}; };
} }

View File

@@ -0,0 +1,21 @@
//
// AsyncDisplayKit+Debug.h
// AsyncDisplayKit
//
// Created by Hannah Troisi on 3/7/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ASImageNode.h"
@interface ASImageNode (Debug)
/**
* Class method to enable visualization of an ASImageNode's image size. For app debugging purposes only.
* @param enabled Specify YES to turn on this debug feature when messaging the ASImageNode class.
*/
+ (void)setImageDebugEnabled:(BOOL)enable;
+ (BOOL)shouldShowImageDebugOverlay;
@end

View File

@@ -0,0 +1,27 @@
//
// AsyncDisplayKit+Debug.m
// AsyncDisplayKit
//
// Created by Hannah Troisi on 3/7/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import "AsyncDisplayKit+Debug.h"
#import "ASDisplayNode+Subclasses.h"
#import "ASTextNode.h"
static BOOL __enableImageSizeOverlay = NO;
@implementation ASImageNode (Debug)
+ (void)setImageDebugEnabled:(BOOL)enable;
{
__enableImageSizeOverlay = enable;
}
+ (BOOL)shouldShowImageDebugOverlay
{
return __enableImageSizeOverlay;
}
@end