From 17d956493a97efb38383db7a9ea308474d8997b5 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Thu, 4 May 2017 16:56:26 -0700 Subject: [PATCH] [ASImageNode] Move debug label and will- / didDisplayNodeContentWithRenderingContext out of drawing method #trivial (#235) * Move configuring the debug label from the drawing method in displayDidFinish * Move will- and didDisplayNodeContentWithRenderingContext in drawing parameters --- Source/ASImageNode.mm | 71 ++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/Source/ASImageNode.mm b/Source/ASImageNode.mm index 267ce70e3..e933461ad 100644 --- a/Source/ASImageNode.mm +++ b/Source/ASImageNode.mm @@ -53,6 +53,8 @@ CGRect cropRect; CGRect cropDisplayBounds; asimagenode_modification_block_t imageModificationBlock; + ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext; + ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext; }; /** @@ -65,8 +67,8 @@ @interface ASImageNodeContentsKey : NSObject {} @property CGRect imageDrawRect; @property BOOL isOpaque; @property (nonatomic, strong) UIColor *backgroundColor; -@property (nonatomic, copy) ASDisplayNodeContextModifier preContextBlock; -@property (nonatomic, copy) ASDisplayNodeContextModifier postContextBlock; +@property (nonatomic, copy) ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext; +@property (nonatomic, copy) ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext; @property (nonatomic, copy) asimagenode_modification_block_t imageModificationBlock; @end @@ -90,8 +92,8 @@ - (BOOL)isEqual:(id)object && CGRectEqualToRect(_imageDrawRect, other.imageDrawRect) && _isOpaque == other.isOpaque && [_backgroundColor isEqual:other.backgroundColor] - && _preContextBlock == other.preContextBlock - && _postContextBlock == other.postContextBlock + && _willDisplayNodeContentWithRenderingContext == other.willDisplayNodeContentWithRenderingContext + && _didDisplayNodeContentWithRenderingContext == other.didDisplayNodeContentWithRenderingContext && _imageModificationBlock == other.imageModificationBlock; } else { return NO; @@ -106,8 +108,8 @@ - (NSUInteger)hash CGRect imageDrawRect; BOOL isOpaque; NSUInteger backgroundColorHash; - void *preContextBlock; - void *postContextBlock; + void *willDisplayNodeContentWithRenderingContext; + void *didDisplayNodeContentWithRenderingContext; void *imageModificationBlock; } data = { _image.hash, @@ -115,8 +117,8 @@ - (NSUInteger)hash _imageDrawRect, _isOpaque, _backgroundColor.hash, - (void *)_preContextBlock, - (void *)_postContextBlock, + (void *)_willDisplayNodeContentWithRenderingContext, + (void *)_didDisplayNodeContentWithRenderingContext, (void *)_imageModificationBlock }; return ASHashBytes(&data, sizeof(data)); @@ -257,7 +259,7 @@ - (void)_locked_setImage:(UIImage *)image [self addSubnode:_debugLabelNode]; }); } - + } else { self.contents = nil; } @@ -299,7 +301,9 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer .forcedSize = _forcedSize, .cropRect = _cropRect, .cropDisplayBounds = _cropDisplayBounds, - .imageModificationBlock = _imageModificationBlock + .imageModificationBlock = _imageModificationBlock, + .willDisplayNodeContentWithRenderingContext = _willDisplayNodeContentWithRenderingContext, + .didDisplayNodeContentWithRenderingContext = _didDisplayNodeContentWithRenderingContext }; return nil; @@ -335,12 +339,12 @@ - (UIImage *)displayWithParameters:(id)parameter isCancelled:(asdispla CGRect cropDisplayBounds = drawParameter.cropDisplayBounds; CGRect cropRect = drawParameter.cropRect; asimagenode_modification_block_t imageModificationBlock = drawParameter.imageModificationBlock; + ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext = drawParameter.willDisplayNodeContentWithRenderingContext; + ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext = drawParameter.didDisplayNodeContentWithRenderingContext; BOOL hasValidCropBounds = cropEnabled && !CGRectIsEmpty(cropDisplayBounds); CGRect bounds = (hasValidCropBounds ? cropDisplayBounds : drawParameterBounds); - ASDisplayNodeContextModifier preContextBlock = self.willDisplayNodeContentWithRenderingContext; - ASDisplayNodeContextModifier postContextBlock = self.didDisplayNodeContentWithRenderingContext; ASDisplayNodeAssert(contentsScale > 0, @"invalid contentsScale at display time"); @@ -357,19 +361,6 @@ - (UIImage *)displayWithParameters:(id)parameter isCancelled:(asdispla CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale); CGSize boundsSizeInPixels = CGSizeMake(std::floor(bounds.size.width * contentsScale), std::floor(bounds.size.height * contentsScale)); - if (_debugLabelNode) { - CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height); - if (pixelCountRatio != 1.0) { - NSString *scaleString = [NSString stringWithFormat:@"%.2fx", pixelCountRatio]; - _debugLabelNode.attributedText = [[NSAttributedString alloc] initWithString:scaleString attributes:[self debugLabelAttributes]]; - _debugLabelNode.hidden = NO; - [self setNeedsLayout]; - } else { - _debugLabelNode.hidden = YES; - _debugLabelNode.attributedText = nil; - } - } - BOOL contentModeSupported = contentMode == UIViewContentModeScaleAspectFill || contentMode == UIViewContentModeScaleAspectFit || contentMode == UIViewContentModeCenter; @@ -414,8 +405,8 @@ - (UIImage *)displayWithParameters:(id)parameter isCancelled:(asdispla contentsKey.imageDrawRect = imageDrawRect; contentsKey.isOpaque = isOpaque; contentsKey.backgroundColor = backgroundColor; - contentsKey.preContextBlock = preContextBlock; - contentsKey.postContextBlock = postContextBlock; + contentsKey.willDisplayNodeContentWithRenderingContext = willDisplayNodeContentWithRenderingContext; + contentsKey.didDisplayNodeContentWithRenderingContext = didDisplayNodeContentWithRenderingContext; contentsKey.imageModificationBlock = imageModificationBlock; if (isCancelled()) { @@ -479,8 +470,8 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd BOOL contextIsClean = YES; CGContextRef context = UIGraphicsGetCurrentContext(); - if (context && key.preContextBlock) { - key.preContextBlock(context); + if (context && key.willDisplayNodeContentWithRenderingContext) { + key.willDisplayNodeContentWithRenderingContext(context); contextIsClean = NO; } @@ -511,8 +502,8 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd [image drawInRect:key.imageDrawRect blendMode:blendMode alpha:1]; } - if (context && key.postContextBlock) { - key.postContextBlock(context); + if (context && key.didDisplayNodeContentWithRenderingContext) { + key.didDisplayNodeContentWithRenderingContext(context); } // The following `UIGraphicsGetImageFromCurrentImageContext` call will commonly take more than 20ms on an @@ -540,7 +531,25 @@ - (void)displayDidFinish __instanceLock__.lock(); void (^displayCompletionBlock)(BOOL canceled) = _displayCompletionBlock; UIImage *image = _image; + BOOL hasDebugLabel = (_debugLabelNode != nil); __instanceLock__.unlock(); + + // Update the debug label if necessary + if (hasDebugLabel) { + // For debugging purposes we don't care about locking for now + CGSize imageSize = image.size; + CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale); + CGSize boundsSizeInPixels = CGSizeMake(std::floor(self.bounds.size.width * self.contentsScale), std::floor(self.bounds.size.height * self.contentsScale)); + CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height); + if (pixelCountRatio != 1.0) { + NSString *scaleString = [NSString stringWithFormat:@"%.2fx", pixelCountRatio]; + _debugLabelNode.attributedText = [[NSAttributedString alloc] initWithString:scaleString attributes:[self debugLabelAttributes]]; + _debugLabelNode.hidden = NO; + } else { + _debugLabelNode.hidden = YES; + _debugLabelNode.attributedText = nil; + } + } // If we've got a block to perform after displaying, do it. if (image && displayCompletionBlock) {