Как переопределить метод рисования в PDFAnnotation IOS -PDFKIT - PullRequest
0 голосов
/ 06 августа 2020

Я следил за другим сообщением StackOverflow, в котором объясняется, как я могу переопределить метод рисования PDFAnnotation, чтобы я мог рисовать изображение вместо традиционного 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

Кто-нибудь знает, как я могу переопределить метод рисования, чтобы я мог нарисовать собственную аннотацию?

Спасибо!

ps: я также пытался следовать руководству на сайте Apple dev.

ОБНОВЛЕНИЕ:

Теперь я могу рисовать картинки, используя CGContextDrawImage, но не могу вернуть координаты на место. когда я это делаю, mi картинки не рисуются и кажется, что они вынесены за пределы страницы, но я не уверен.

Это мой новый код:

- (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();
}

1 Ответ

1 голос
/ 06 августа 2020

Я также пытался следовать руководству на сайте разработчиков 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();
}

enter image description here

The image was drawn with CGRectMake(20, 20, 100, 100). It's upside down, because PDFPage coordinates are flipped (0, 0 = bottom/left). Leaving it as an exercise for OP.

Rotation

Your rotation code is wrong:

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

It's based on _pdfView bounds, but it should be based on the image bounds (_bounds). Here's the correct one:

- (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();
}

введите описание изображения здесь

...