【问题标题】:How to override draw method on PDFAnnotation IOS-PDFKIT如何覆盖 PDFAnnotation IOS-PDFKIT 上的绘制方法
【发布时间】: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();
}

【问题讨论】:

    标签: objective-c ios-pdfkit


    【解决方案1】:

    我也尝试按照 Apple 开发网站上的教程进行操作。

    哪一个?

    因为两者都包含 UIGraphicsPushContext(context)CGContextSaveGState(context) 调用,但您的代码不包含。不要盲目复制和粘贴示例,尝试理解它们。阅读这两个调用的作用。

    固定代码:

    - (void)drawWithBox:(PDFDisplayBox) box
              inContext:(CGContextRef)context {
        [super drawWithBox:box inContext:context];
        
        UIGraphicsPushContext(context);
        CGContextSaveGState(context);
        
        [_picture drawInRect:_bounds];
    
        CGContextRestoreGState(context);
        UIGraphicsPopContext();
    }
    

    图像是使用CGRectMake(20, 20, 100, 100) 绘制的。它是颠倒的,因为PDFPage 坐标被翻转(0, 0 = 底部/左侧)。把它作为 OP 的练习。

    旋转

    您的轮换代码错误:

    CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
    CGContextScaleCTM(context, 1.0,  -1.0);
        
    CGContextDrawImage(context, _bounds, _picture.CGImage);
    

    它基于_pdfView 边界,但它应该基于图像边界 (_bounds)。这是正确的:

    - (void)drawWithBox:(PDFDisplayBox) box
              inContext:(CGContextRef)context {
        [super drawWithBox:box inContext:context];
        
        UIGraphicsPushContext(context);
        CGContextSaveGState(context);
    
        CGContextTranslateCTM(context, _bounds.origin.x, _bounds.origin.y + _bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        [_picture drawInRect:CGRectMake(0, 0, _bounds.size.width, _bounds.size.height)];
    
        CGContextRestoreGState(context);
        UIGraphicsPopContext();
    }
    

    【讨论】:

    • 感谢您的帮助!同样在阅读了很多关于我的问题后,我发现 CGContextDrawImage() 在我的情况下非常有效^^
    • 我尝试使用CGContextTranslateCTM 和`` CGContextScaleCTM`` 来翻转PDFPage 坐标,正如苹果网站上所解释的那样,但之后我无法绘制任何东西。我想我做错了什么,但我不知道是什么
    • 查看更新的答案,您使用了错误的框架。您应该使用要在其中绘制图像的框架。
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多