ASImageNodeRoundBorderModificationBlock().

This commit is contained in:
Nadine Salter 2014-11-19 17:14:17 -08:00
parent 1e929c3ead
commit 964d109be6
2 changed files with 47 additions and 3 deletions

View File

@ -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 <imageModificationBlock>
*
* @returns An ASImageNode image modification block.
*/
asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor);
ASDISPLAYNODE_EXTERN_C_END

View File

@ -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;
};
}