[ASDisplayNode] Pass drawParameter in rendering context callbacks (#248)

* Pass drawParameter in rendering context callbacks

* Add changelog

* Fix examples
This commit is contained in:
Michael Schneider
2017-05-08 14:39:40 -07:00
committed by GitHub
parent ad9924abd8
commit 8692428481
6 changed files with 17 additions and 13 deletions

View File

@@ -12,3 +12,4 @@
- Move more properties from ASTableView, ASCollectionView to their respective node classes. [Adlai Holler](https://github.com/Adlai-Holler) - Move more properties from ASTableView, ASCollectionView to their respective node classes. [Adlai Holler](https://github.com/Adlai-Holler)
- Remove finalLayoutElement [Michael Schneider] (https://github.com/maicki)[#96](https://github.com/TextureGroup/Texture/pull/96) - Remove finalLayoutElement [Michael Schneider] (https://github.com/maicki)[#96](https://github.com/TextureGroup/Texture/pull/96)
- Add ASPageTable - A map table for fast retrieval of objects within a certain page [Huy Nguyen](https://github.com/nguyenhuy) - Add ASPageTable - A map table for fast retrieval of objects within a certain page [Huy Nguyen](https://github.com/nguyenhuy)
- [ASDisplayNode] Pass drawParameter in rendering context callbacks [Michael Schneider](https://github.com/maicki)[#248](https://github.com/TextureGroup/Texture/pull/248)

View File

@@ -56,7 +56,7 @@ typedef void (^ASDisplayNodeDidLoadBlock)(__kindof ASDisplayNode * node);
/** /**
* ASDisplayNode will / did render node content in context. * ASDisplayNode will / did render node content in context.
*/ */
typedef void (^ASDisplayNodeContextModifier)(CGContextRef context); typedef void (^ASDisplayNodeContextModifier)(CGContextRef context, id _Nullable drawParameters);
/** /**
* ASDisplayNode layout spec block. This block can be used instead of implementing layoutSpecThatFits: in subclass * ASDisplayNode layout spec block. This block can be used instead of implementing layoutSpecThatFits: in subclass

View File

@@ -413,8 +413,11 @@ struct ASImageNodeDrawParameters {
return nil; return nil;
} }
ASWeakMapEntry<UIImage *> *entry = [self.class contentsForkey:contentsKey isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled]; ASWeakMapEntry<UIImage *> *entry = [self.class contentsForkey:contentsKey
if (entry == nil) { // If nil, we were cancelled. drawParameters:parameter
isCancelled:isCancelled];
// If nil, we were cancelled.
if (entry == nil) {
return nil; return nil;
} }
@@ -428,7 +431,7 @@ struct ASImageNodeDrawParameters {
static ASWeakMap<ASImageNodeContentsKey *, UIImage *> *cache = nil; static ASWeakMap<ASImageNodeContentsKey *, UIImage *> *cache = nil;
static ASDN::Mutex cacheLock; static ASDN::Mutex cacheLock;
+ (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled + (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{ {
{ {
ASDN::MutexLocker l(cacheLock); ASDN::MutexLocker l(cacheLock);
@@ -443,7 +446,7 @@ static ASDN::Mutex cacheLock;
} }
// cache miss // cache miss
UIImage *contents = [self createContentsForkey:key isCancelled:isCancelled]; UIImage *contents = [self createContentsForkey:key drawParameters:drawParameters isCancelled:isCancelled];
if (contents == nil) { // If nil, we were cancelled if (contents == nil) { // If nil, we were cancelled
return nil; return nil;
} }
@@ -454,7 +457,7 @@ static ASDN::Mutex cacheLock;
} }
} }
+ (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{ {
// The following `UIGraphicsBeginImageContextWithOptions` call will sometimes take take longer than 5ms on an // The following `UIGraphicsBeginImageContextWithOptions` call will sometimes take take longer than 5ms on an
// A5 processor for a 400x800 backingSize. // A5 processor for a 400x800 backingSize.
@@ -471,7 +474,7 @@ static ASDN::Mutex cacheLock;
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRef context = UIGraphicsGetCurrentContext();
if (context && key.willDisplayNodeContentWithRenderingContext) { if (context && key.willDisplayNodeContentWithRenderingContext) {
key.willDisplayNodeContentWithRenderingContext(context); key.willDisplayNodeContentWithRenderingContext(context, drawParameters);
contextIsClean = NO; contextIsClean = NO;
} }
@@ -503,7 +506,7 @@ static ASDN::Mutex cacheLock;
} }
if (context && key.didDisplayNodeContentWithRenderingContext) { if (context && key.didDisplayNodeContentWithRenderingContext) {
key.didDisplayNodeContentWithRenderingContext(context); key.didDisplayNodeContentWithRenderingContext(context, drawParameters);
} }
// The following `UIGraphicsGetImageFromCurrentImageContext` call will commonly take more than 20ms on an // The following `UIGraphicsGetImageFromCurrentImageContext` call will commonly take more than 20ms on an
@@ -517,7 +520,7 @@ static ASDN::Mutex cacheLock;
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
if (key.imageModificationBlock != NULL) { if (key.imageModificationBlock) {
result = key.imageModificationBlock(result); result = key.imageModificationBlock(result);
} }

View File

@@ -251,7 +251,7 @@
// For -display methods, we don't have a context, and thus will not call the _willDisplayNodeContentWithRenderingContext or // For -display methods, we don't have a context, and thus will not call the _willDisplayNodeContentWithRenderingContext or
// _didDisplayNodeContentWithRenderingContext blocks. It's up to the implementation of -display... to do what it needs. // _didDisplayNodeContentWithRenderingContext blocks. It's up to the implementation of -display... to do what it needs.
if (willDisplayNodeContentWithRenderingContext != nil) { if (willDisplayNodeContentWithRenderingContext != nil) {
willDisplayNodeContentWithRenderingContext(currentContext); willDisplayNodeContentWithRenderingContext(currentContext, drawParameters);
} }
// Decide if we use a class or instance method to draw or display. // Decide if we use a class or instance method to draw or display.
@@ -266,7 +266,7 @@
} }
if (didDisplayNodeContentWithRenderingContext != nil) { if (didDisplayNodeContentWithRenderingContext != nil) {
didDisplayNodeContentWithRenderingContext(currentContext); didDisplayNodeContentWithRenderingContext(currentContext, drawParameters);
} }
if (shouldCreateGraphicsContext) { if (shouldCreateGraphicsContext) {

View File

@@ -74,7 +74,7 @@ class PhotoWithInsetTextOverlay : LayoutExampleNode {
backgroundColor = .clear backgroundColor = .clear
photoNode.url = URL(string: "http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png") photoNode.url = URL(string: "http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png")
photoNode.willDisplayNodeContentWithRenderingContext = { context in photoNode.willDisplayNodeContentWithRenderingContext = { context, drawParameters in
let bounds = context.boundingBoxOfClipPath let bounds = context.boundingBoxOfClipPath
UIBezierPath(roundedRect: bounds, cornerRadius: 10).addClip() UIBezierPath(roundedRect: bounds, cornerRadius: 10).addClip()
} }

View File

@@ -125,7 +125,7 @@
_photoNode = [[ASNetworkImageNode alloc] init]; _photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png"]; _photoNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png"];
_photoNode.willDisplayNodeContentWithRenderingContext = ^(CGContextRef context) { _photoNode.willDisplayNodeContentWithRenderingContext = ^(CGContextRef context, id drawParameters) {
CGRect bounds = CGContextGetClipBoundingBox(context); CGRect bounds = CGContextGetClipBoundingBox(context);
[[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:10] addClip]; [[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:10] addClip];
}; };