如何在ios中进行自定义绘图?

时间:2016-01-11 09:46:22

标签: ios iphone uikit

我想制作自定义绘图,以便将其转换为图像。

enter image description here

我听说过UIBezierPath,但我不太了解它,我的目的是根据用户的颜色选择来改变它的颜色。

1 个答案:

答案 0 :(得分:0)

创建CGGraphcisContext并获取如下图像:

UIGraphicsBeginImageContextWithOptions(bounds.size, NO , [[UIScreen mainScreen] scale]);

// set the fill color (UIColor *)
[userSelectedColor setFill];

//create your path
UIBezierPath *path = ...

//fill the path with your color
[path fill]; 

UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

您可能需要组合多个路径才能获得所需的形状。首先创建' drop'与bezier路径。路径可能如下所示:

//Create the top half of the circle
UIBezierPath *drop = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(bounds)*0.5f, CGRectGetWidth(bounds)*0.5f)
                       radius:CGRectGetWidth(bounds)*0.5f
                       startAngle:0
                       endAngle:DEGREES_TO_RADIANS(180)
                       clockwise:NO];

//Add the first half of the bottom part
[drop addCurveToPoint:CGPointMake(CGRectGetWidth(bounds)*0.5f,CGRectGetHeight(bounds)) 
        controlPoint1:CGPointMake(CGRectGetWidth(bounds),CGRectGetWidth(bounds)*0.5f+CGRectGetHeight(bounds)*0.1f)]
        controlPoint2:CGPointMake(CGRectGetWidth(bounds)*0.6f,CGRectGetHeight(bounds)*0.8f)];

//Add the second half of the bottom part beginning from the sharp corner
[drop addCurveToPoint:CGPointMake(0,CGRectGetWidth(bounds)*0.5f) 
        controlPoint1:CGPointMake(CGRectGetWidth(bounds)*0.4f,CGRectGetHeight(bounds)*0.8f)
       controlPoint2:CGPointMake(0,CGRectGetWidth(bounds)*0.5f+CGRectGetHeight(bounds)*0.1f)];

[drop closePath];

不完全确定这是否有效,因为我现在无法测试它。您可能需要稍微使用控制点。可能是我在方向上犯了一些错误。