diff --git a/AsyncDisplayKit/ASImageNode.h b/AsyncDisplayKit/ASImageNode.h index 165d8c2449..d1c4aa840b 100644 --- a/AsyncDisplayKit/ASImageNode.h +++ b/AsyncDisplayKit/ASImageNode.h @@ -27,9 +27,12 @@ typedef NS_ENUM(NSUInteger, ASImageNodeTint) { * Image modification block. Use to transform an image before display. * * @param image The image to be displayed. + * + * @returns A transformed image. */ typedef UIImage *(^asimagenode_modification_block_t)(UIImage *image); + /** * @abstract Draws images. * @discussion Supports cropping, tinting, and arbitrary image modification blocks. @@ -50,8 +53,6 @@ typedef UIImage *(^asimagenode_modification_block_t)(UIImage *image); */ @property (nonatomic, assign) ASImageNodeTint tint; -#pragma mark - Cropping - /** * @abstract Indicates whether efficient cropping of the receiver is enabled. * @@ -100,7 +101,6 @@ typedef UIImage *(^asimagenode_modification_block_t)(UIImage *image); */ @property (nonatomic, readwrite, copy) asimagenode_modification_block_t imageModificationBlock; -#pragma mark - /** * @abstract Marks the receiver as needing display and performs a block after * display has finished. @@ -116,3 +116,20 @@ typedef UIImage *(^asimagenode_modification_block_t)(UIImage *image); - (void)setNeedsDisplayWithCompletion:(void (^)(BOOL canceled))displayCompletionBlock; @end + + +ASDISPLAYNODE_EXTERN_C_BEGIN + +/** + * @abstract Image modification block that rounds (and optionally adds a border to) an image. + * + * @param borderWidth The width of the round border to draw, or zero if no border is desired. + * @param borderColor What colour border to draw. + * + * @see + * + * @returns An ASImageNode image modification block. + */ +asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor); + +ASDISPLAYNODE_EXTERN_C_END diff --git a/AsyncDisplayKit/ASImageNode.mm b/AsyncDisplayKit/ASImageNode.mm index f64bc6d9f3..41b8c6b6e9 100644 --- a/AsyncDisplayKit/ASImageNode.mm +++ b/AsyncDisplayKit/ASImageNode.mm @@ -335,3 +335,30 @@ } @end + + +#pragma mark - Extras +extern asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor) +{ + return ^(UIImage *originalImage) { + UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, originalImage.scale); + UIBezierPath *roundOutline = [UIBezierPath bezierPathWithOvalInRect:(CGRect){CGPointZero, originalImage.size}]; + + // Make the image round + [roundOutline addClip]; + + // Draw the original image + [originalImage drawAtPoint:CGPointZero]; + + // Draw a border on top. + if (borderWidth > 0.0) { + [borderColor setStroke]; + CGContextSetLineWidth(UIGraphicsGetCurrentContext(), borderWidth); + [roundOutline stroke]; + } + + UIImage *modifiedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return modifiedImage; + }; +}