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

问题描述

我正在开发一个 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/像素。

解决方法

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...