Drawing geometry (circle or rect) on zoomable CGPDF document using QuartzCore

379 views Asked by At


    I am trying to draw on touches (mouse tap) on the pdf document generated by CGContextRef from my resource bundle. I have started from Apple zoomingpdfviewer where I have a UIScrollView and Pdf is generated by CGContextDrawPDFPage(context, pdfPage);

I am have also added a CATiledLayer on the scrollView and it is drawn each time in layoutSubviews - when UIScrollView is zoomed. I am confused a bit that should I draw the mouse points on scrollView CGContext or TileLayers.

--> Moving ahead, I wanted to add a rect/ circle/point on the pdf document where the user taps.
  Just to start with I am :
CGContextMoveToPoint(context, 5,5); CGContextSetLineWidth(context, 12.0f); CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]); CGContextAddRect(context, CGRectMake(50, 50, 20, 50)); CGContextAddLineToPoint(context, 25, 5); CGContextStrokePath(context);

drawing it to the current context. Similarly I plan to draw when user taps in.
  But when I zoom and TitledLayer is redrawn to match the zoom content, these drawn rect/ circle disappears.
If I am not wrong then CATiledLayer is being drawn above the currentContext rects/ circle .
     
   The end functionality is quite similar to the Maps App where the Tile Layers are added but the dropped points are located exactly on the same location even after the map is zoomed.
     
    Quite lost after seeing many such posts as drawing on scrollView , pdf iOS viewer.
    Does anyone know how can I draw geomtry (rect/points) on the pdf document and keep their location exactly same even if PdfView Zoom in and out? Should I convert a pdf into image or any other way to do this?

2

There are 2 answers

0
Vacca On

After searching and trying some stuff on CoreAnimation,CALayer I came up with: If you want to add anything on zoomable PDF viewer,

1) Add a UITapGestureRecognizer for handling single touch

UITapGestureRecognizer *singleTapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapOnScrollView:)];
    singleTapGestureRecognizer.numberOfTapsRequired=1;
    singleTapGestureRecognizer.delegate=self;
    [self addGestureRecognizer:singleTapGestureRecognizer];
    [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer]; // added if you have a UITapGestureRecognizer for double taps

2) Simply add the new views (circle,rect any geometry) as layers on the Tiled PDF view

[self.titledPdfView.layer addSublayer:view1.layer];

where you can subclass your geometry views to draw with CGContext currentContext. This automatically reposition the layers when pdf is zoomed in/out

I was trying to add the views by drawing context on drawLayer:(CALayer*)layer inContext:(CGContextRef)context of titledPdfView class which gave a positional error

You can also implement a TouchesMoved, TouchesEnded method if you want to draw a resizable TextView, arow, Line.

I hope this helps for someone who needs to draw customize objects on Apple's ZoomingPDFViewer.

0
Vacca On

Further to add (if anyone arrives here), replace the Apple code of
  TiledPdfView *titledPDFView = [[TiledPdfView alloc] initWithFrame:pageRect scale:pdfScale]; [titledPDFView setPage:pdfPage]; [self addSubview:titledPdfView]; [titledPDFView setNeedsDisplay]; [self bringSubviewToFront:titledPdfView]; self.titledPdfView = titledPDFView;
  With :

titledPdfView = [[TiledPdfView alloc] initWithFrame:pageRect scale:pdfScale];
[titledPdfView setPage:pdfPage];
[self addSubview:titledPdfView];
[self bringSubviewToFront:titledPdfView];

I dont know why have they added a view like that (member object to class object) which restricts the
  -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
being before zooming. I have tried with setNeedsDisplay with no effect then replaced n it works.
  Esp. this is annoying if you want to draw annots on your TileLayer and nothing appears. Hope this helps!!