在iOS中,我正在尝试绘制剪裁的图像。我希望裁剪是从一个边缘,弯曲到最远的相对边缘。下图以红色显示最终图像的边框。在此示例中,曲线从左下角到右上角。
这是我目前编写的代码。我唯一的问题是这个方法bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
没有曲线到我需要的范围。
- (UIImage *)roundCorneredImage:(UIImage *)image radius:(CGFloat)radius {
CGRect imageRect = CGRectZero;
imageRect.size = image.size;
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale);
CGSize size = CGSizeMake(radius, radius);
[[UIBezierPath bezierPathWithRoundedRect:imageRect byRoundingCorners:UIRectCornerBottomRight cornerRadii:size] addClip];
[image drawInRect:imageRect];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
更新:
这是我得到的结果和我更新的代码。我需要一些进一步的帮助...
UIBezierPath* bezier = [UIBezierPath bezierPath];
[bezier moveToPoint:CGPointMake(0, 0)];
[bezier addLineToPoint:CGPointMake(0, imageRect.size.height)];
[bezier addArcWithCenter:CGPointMake(imageRect.size.width / 2, imageRect.size.height / 2)
radius:imageRect.size.height / 2
startAngle:M_PI / 2
endAngle:0
clockwise:NO];
[bezier addLineToPoint:CGPointMake(0, 0)];
[bezier addClip];
答案 0 :(得分:1)
添加addLineToPoint
的直线和addArcWithCenter:radius:startAngle:endAngle:clockwise:
的曲线,以便控制曲线的形状。
答案 1 :(得分:1)
对于想要查看示例代码的任何其他人来说,这是我的解决方案。
- (UIImage *)roundCorneredImage:(UIImage *)image radius:(CGFloat)radius {
CGRect imageRect = CGRectZero;
imageRect.size = image.size;
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale);
UIBezierPath* bezier = [UIBezierPath bezierPath];
[bezier moveToPoint:CGPointMake(imageRect.size.width, 0)];
[bezier addLineToPoint:CGPointMake(0, 0)];
[bezier addLineToPoint:CGPointMake(0, imageRect.size.height)];
[bezier addArcWithCenter:CGPointMake(0, 0) radius:imageRect.size.height startAngle:M_PI / 2 endAngle:0 clockwise:NO];
[bezier addClip];
[image drawInRect:imageRect];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}