如何在ios中制作自定义图形?

如何在ios中制作自定义图形?,ios,iphone,uikit,Ios,Iphone,Uikit,我想制作自定义图形,以便将其转换为图像 我听说过UIBezierPath,但对它了解不多,我的目的是根据用户选择的颜色来更改它的颜色。创建一个CGGraphDisContext并获得如下图像: UIGraphicsBeginImageContextWithOptions(bounds.size, NO , [[UIScreen mainScreen] scale]); // set the fill color (UIColor *) [userSelectedColor setFill];

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


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

创建一个CGGraphDisContext并获得如下图像:

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();
//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];
您可能必须组合多个路径才能获得所需的形状。首先创建带有bezier路径的“drop”。路径可能如下所示:

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();
//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];
不完全确定这是否有效,因为我现在无法测试它。您可能需要稍微使用控制点。可能是我在方向上犯了一些错误