繁体   English   中英

将UIBezierPath转换为UIImage

[英]Convert UIBezierPath to UIImage

我正在创建Apple Watch,因此附带了一个Iphone应用程序,其中使用UIBezierPath绘制了一个饼图。 而且我需要将该图转换为UIImage 这是我尝试的代码:

GraphView.m

 rectImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];

 CGContextRef context = UIGraphicsGetCurrentContext();
 UIGraphicsPushContext(context);
 UIGraphicsBeginImageContext(rectImage.frame.size);

 CGFloat arcRadious = OUTER_RADIOUS;
 CGFloat tmpStartAngle = startAngle;
 CGFloat tmpEndAngle = endAngle;

 UIBezierPath *path = [[UIBezierPath alloc] init];
 path.lineWidth = 23;

 [path addArcWithCenter:centre radius:arcRadious startAngle:tmpStartAngle endAngle:tmpEndAngle clockwise:YES];

 [(UIColor*)_fillColours[index] setStroke];
 [path stroke];

 CGContextAddPath(context, path.CGPath);
 image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsPopContext();
 UIGraphicsEndImageContext();

但是在Apple Watch中,只显示了一部分图表。 谁能告诉我我做错了什么。 任何帮助是极大的赞赏。

这里有些错误-让我们逐一分解。


1:创建上下文

CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
UIGraphicsBeginImageContext(rectImage.frame.size);

我不确定您要在这里做什么。

您正在做的是从堆栈顶部(可能甚至不存在)获取当前上下文,然后尝试将该上下文再次推入堆栈-这毫无意义,因为它已经在堆栈上了(当前上下文!)。

然后,您将创建图像上下文,它将自动将其推入堆栈-使其成为当前上下文。

因此,我假设您的意思是:

UIGraphicsBeginImageContext(rectImage.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();

这将创建一个新的图像上下文,使其成为当前上下文,然后获得对其的引用。


2:创建路径

UIBezierPath *path = [[UIBezierPath alloc] init];
[path addArcWithCenter:centre radius:arcRadious startAngle:tmpStartAngle endAngle:tmpEndAngle clockwise:YES];

这可能是有问题的,因为在添加弧之前您永远不会将路径移动到点-因此可能无法正确绘制。

因此,在添加之前,您需要将其移动到圆弧的中心点。

UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:centre];
[path addArcWithCenter:centre radius:arcRadious startAngle:tmpStartAngle endAngle:tmpEndAngle clockwise:YES];

3:画出道路

[(UIColor*)_fillColours[index] setStroke];
[path stroke];

CGContextAddPath(context, path.CGPath); // <- Remove

您的第三行是不必要的。 这是因为UIBezierPath上的stroke方法实际上将UIBezierPath路径添加到当前上下文,然后在该上下文中UIBezierPath该路径。


4:结束上下文

UIGraphicsPopContext(); // <- Remove
UIGraphicsEndImageContext();

同样,当您调用UIGraphicsEndImageContext()时,图像上下文将处理自身从堆栈弹出的问题,因此尝试自行弹出图像可能会导致问题。


最后,一些学究的细节。

rectImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];

看到您正在使用此代码处理图像时,名称rectImage确实具有误导性,因为它是UIView 我将重命名为rectView

CGFloat arcRadious = OUTER_RADIOUS;

我衷心希望OUTER_RADIOUS不是宏( #define )。 常量应该用宏定义-类型不安全,可能会使调试成为噩梦。 避免他们像瘟疫一样。

相反,您应该使用static C常量。 例如:

static CGFloat const kOuterRadius = 100.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM