如何使用核心图形绘制带有自定义图案/图像的线条/笔划?

时间:2021-01-08 19:50:02

标签: ios swift pdfkit ios-pdfkit ios-pdfkit-outlines

我正在开发一个 pdf 注释应用程序,现在我可以使用“PDFAnnotationSubtype.stamp”中的图像画一条线。

图片: https://codepen.io/team/amcharts/pen/ergXmr?editors=1111

使用以下代码:

public class ImageAnnotation: PDFAnnotation{ 

 init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
    super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)

    self._image = image
}

override public func draw(with box: PDFDisplayBox, in context: CGContext) {
    guard let cgImage = self._image?.cgImage else { return }
    context.draw(cgImage, in: self.bounds)
}
}

结果: enter image description here

使用这种方法我最终会在每个 CGpoint 添加图像(换句话说,每个 cgpoint 都是单独的 PDFAnnotation)。我如何将整个路径作为单个笔划/单个注释,以便当我尝试擦除时,整个路径应该被清除,而不是只有几个 cgpoint/像素。

1 个答案:

答案 0 :(得分:1)

在搜索了几个资源后发现我们可以使用“CGPattern”来纹理化线条。

参考: https://developer.apple.com/documentation/coregraphics/cgpattern

对于尝试此操作的任何其他人,请确保使用“填充”和“描边”模式玩“drawingPath”。