Created
November 10, 2018 11:46
-
-
Save manajay/0ce0bf2ed59b61b768562a4074abc2a4 to your computer and use it in GitHub Desktop.
创建圆角的图片遮罩, insertSubview 到底部
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage *)createRoundedCornerImageWithRadius:(CGFloat)radius outerSize:(CGSize)outerSize innerSize:(CGSize)innerSize fillColor:(UIColor *)fillColor { | |
UIGraphicsBeginImageContextWithOptions(outerSize, false, [UIScreen mainScreen].scale); | |
CGContextRef currentContext = UIGraphicsGetCurrentContext(); | |
UIBezierPath *bezierPath = [[UIBezierPath alloc] init]; | |
CGFloat xOffset = (outerSize.width - innerSize.width) / 2; | |
CGFloat yOffset = (outerSize.height - innerSize.height) / 2; | |
CGPoint hLeftUpPoint = CGPointMake(xOffset + radius, yOffset); | |
CGPoint hRightUpPoint = CGPointMake(outerSize.width - xOffset - radius, yOffset); | |
CGPoint hLeftDownPoint = CGPointMake(xOffset + radius, outerSize.height - yOffset); | |
CGPoint vLeftUpPoint = CGPointMake(xOffset, yOffset + radius); | |
CGPoint vRightDownPoint = CGPointMake(outerSize.width - xOffset, outerSize.height - yOffset - radius); | |
CGPoint centerLeftUp = CGPointMake(xOffset + radius, yOffset + radius); | |
CGPoint centerRightUp = CGPointMake(outerSize.width - xOffset - radius, yOffset + radius); | |
CGPoint centerLeftDown = CGPointMake(xOffset + radius, outerSize.height - yOffset - radius); | |
CGPoint centerRightDown = CGPointMake(outerSize.width - xOffset - radius, outerSize.height - yOffset - radius); | |
[bezierPath moveToPoint:hLeftUpPoint]; | |
[bezierPath addLineToPoint:hRightUpPoint]; | |
[bezierPath addArcWithCenter:centerRightUp radius:radius startAngle:M_PI * 3 / 2 endAngle:M_PI * 2 clockwise:YES]; | |
[bezierPath addLineToPoint:vRightDownPoint]; | |
[bezierPath addArcWithCenter: centerRightDown radius: radius startAngle: 0 endAngle: M_PI / 2 clockwise: YES]; | |
[bezierPath addLineToPoint: hLeftDownPoint]; | |
[bezierPath addArcWithCenter:centerLeftDown radius: radius startAngle: M_PI / 2 endAngle: M_PI clockwise:YES]; | |
[bezierPath addLineToPoint:vLeftUpPoint]; | |
[bezierPath addArcWithCenter:centerLeftUp radius: radius startAngle: M_PI endAngle: M_PI * 3 / 2 clockwise: YES]; | |
[bezierPath addLineToPoint: hLeftUpPoint]; | |
[bezierPath closePath]; | |
//If draw drection of outer path is same with inner path, final result is just outer path. | |
[bezierPath moveToPoint: CGPointZero]; | |
[bezierPath addLineToPoint:CGPointMake(0, outerSize.height)]; | |
[bezierPath addLineToPoint:CGPointMake(outerSize.width, outerSize.height)]; | |
[bezierPath addLineToPoint:CGPointMake(outerSize.width, 0)]; | |
[bezierPath addLineToPoint:CGPointZero]; | |
[bezierPath closePath]; | |
[fillColor setFill]; | |
[bezierPath fill]; | |
CGContextDrawPath(currentContext, kCGPathFillStroke); | |
UIImage *antiRoundedCornerImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return antiRoundedCornerImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment