【发布时间】:2020-08-06 12:06:20
【问题描述】:
我关注了另一个 StackOverflow 帖子,该帖子解释了如何覆盖 PDFAnnotation 的 draw 方法,以便我可以绘制图片而不是传统的 PDFAnnotation。
但遗憾的是,我无法做到这一点,并且在我的 pdf 顶部绘制的注释仍然是常规注释。
这是我使用的代码:
@implementation PDFImageAnnotation { UIImage * _picture;
CGRect _bounds;};
-(instancetype)initWithPicture:(nonnull UIImage *)picture bounds:(CGRect) bounds{
self = [super initWithBounds:bounds
forType:PDFAnnotationSubtypeWidget
withProperties:nil];
if(self){
_picture = picture;
_bounds = bounds;
}
return self;
}
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
[_picture drawInRect:_bounds];
CGContextRestoreGState(context);
UIGraphicsPushContext(context);
};
@end
有人知道我可以如何覆盖 draw 方法以便绘制自定义注释吗?
谢谢!
ps:我也尝试按照苹果开发网站上的教程进行操作。
更新:
现在我可以使用CGContextDrawImage 绘制图片,但我无法将坐标翻转回原位。当我这样做时,我的图片没有被绘制出来,而且似乎它们被放在了页面之外,但我不确定。
这是我的新代码:
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, _bounds, _picture.CGImage);
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
【问题讨论】: