0

I followed another StackOverflow post that explains how i could override the draw method of a PDFAnnotation so i could draw a picture instead of a traditional PDFAnnotation.

But sadly i was not able to achieve that and the annotation that is drawn on top of my pdf is still a regular one.

This is the code that i used :

@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

Does someone know how i could override the draw method so i could draw a custom Annotation ?

Thank You !

ps: i also tried to followed the tutorial on the apple dev site.

UPDATE :

Now i'm able to draw pictures using CGContextDrawImage but i'm not able to flip coordinates back in place. when i do that mi pictures are not drawn and it seems that they are put outside of the page but i'm not sure.

This is my new code :

- (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();
}
Michel Melhem
  • 551
  • 1
  • 8
  • 22

1 Answers1

1

I also tried to follow the tutorial on the Apple dev site.

Which one?

Because both include UIGraphicsPushContext(context) & CGContextSaveGState(context) calls, but your code doesn't. Do not blindly copy & paste examples, try to understand them. Read what these two calls do.

Fixed code:

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

enter image description here

zrzka
  • 20,249
  • 5
  • 47
  • 73
  • Thank you for your help ! Also after reading a lot about my issue i found CGContextDrawImage() that work very in my case ^^ – Michel Melhem Aug 06 '20 at 13:58
  • i tried to flip PDFPage coordinates using ```CGContextTranslateCTM``` and ``` CGContextScaleCTM``` as explained on apple's website but after doing that i'm not able to draw anything. I think i made something wrong but i'm not able to figure out what – Michel Melhem Aug 07 '20 at 13:06
  • See updated answer, you're using wrong frame. You should use frame in which you'd like to draw the image. – zrzka Aug 07 '20 at 16:11