【问题标题】:UIBezierPath : Make image of drawing onlyUIBezierPath :仅制作绘图图像
【发布时间】:2018-10-21 21:56:28
【问题描述】:

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

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 对象。但是有了这个,我得到了空白图像。

有什么建议吗?

【问题讨论】:

    标签: ios objective-c uiimage uibezierpath


    【解决方案1】:

    好的,我想通了。

    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) 的图像,而不是整个视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      相关资源
      最近更新 更多