UIBezierPath :仅制作绘图图像

UIBezierPath : Make image of drawing only

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

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();

我在创建图像时为一些填充提供了静态值(给定了一些 space)。

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