UIBezierPath:仅制作图形图像

毛利克

我正在使用以下代码从自由手绘图创建图像:

UIGraphicsBeginImageContext(self.bounds.size);
for (UIBezierPath *path in self.pathArray) {
    [self.lineColor setStroke];
    [path stroke];
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我只想拍摄黑色线条(绘图)而不是整个视图的图像。视图的尺寸为300x300。但是,如果我的绘图是50x50,那么我只想专注于该部分。

我尝试过

UIGraphicsBeginImageContext(signPath.bounds.size);

signPathUIBezierPath对象。但是与此同时我得到空白图像。

有什么建议么 ?

毛利克

好,我知道了。

UIGraphicsBeginImageContext(signPath.bounds.size);

通过这种方式,我仅创建图像上下文的大小,而缺少原点。所以我需要创建图像上下文x and y (origins).

我出身在UIBezierPathsize.

代码:

    CGSize size = signPath.bounds.size;
    size = CGSizeMake(size.width + 10, size.height + 10);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGContextConcatCTM(c, CGAffineTransformMakeTranslation(-signPath.bounds.origin.x + 5, -signPath.bounds.origin.y + 5));
    [self.layer renderInContext:c];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

在创建图像时,我为某些填充(给了一些空间)指定了静态值。

不知道这是否是标准方法,但是它解决了我的问题,并且我能够在500x500的视图中创建手绘图的图像。现在,我得到的是我的自由手绘图(Strokes)的图像,而不是整个视图的图像。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章