问题
I have use [PDFView setNeedsDisplay:YES]
to let the PDF view redraw, and it worked great on OSX 10.9-10.11.
However it doesn't work unless I zoom in or zoom out the PDF page...
Is there any other way to redraw immediately? Code below:
NSRect newBounds;
NSRect currentBounds;
NSRect dirtyRect;
NSPoint mouseLoc;
NSPoint endPt;
// Where is annotation now?
currentBounds = [_activeAnnotation bounds];
// Mouse in display view coordinates.
mouseLoc = [self convertPoint: [theEvent locationInWindow] fromView: NULL];
// Convert end point to page space.
if(activePage == nil)
activePage =[_activeAnnotation page];
_LinePoint= [self convertPoint: mouseLoc toPage: activePage];
endPt = [self convertPoint: mouseLoc toPage: activePage];
if(_selectedIdx == 3) //ink
{
[(PDFAnnotationInk*)_activeAnnotation removeBezierPath:_path];
//endPt.x=_xPoint.x; //竖线
//endPt.y=_xPoint.y; //横线
[_path lineToPoint:endPt]; // 普通笔
[(PDFAnnotationInk*)_activeAnnotation addBezierPath:_path];
[self annotationChanged];
[self setNeedsDisplay:YES];
return;
UPDATE:
I found that the setNeedsDispaly
calls the drawPage:toContext:
however the drawing code doesn't work in drawPage:toContext:
- (void)drawPage:(PDFPage *)pdfPage toContext(CGContextRef)context
{
[super drawPage: pdfPage toContext:context];
NSBezierPath *line=[NSBezierPath bezierPath];
[line moveToPoint:_xPoint];
[line lineToPoint:NSMakePoint(150, 150)];
[[NSColor redColor] set];
[line setLineWidth:50] ;
[line stroke];
}
the debug said CGContextSetFillColorWithColor: invalid context 0x0
and more invalid context 0x0
warnings.
What I do in the drawPage:toContext:
is testing and just use BezierPath to draw a line.
回答1:
I'm having the same trouble. The first time I add an annotation, PDFView
displays that annotation on the page immediately. From then on, adding or removing an annotation works fine in code but PDFView
doesn't show the change until I manually scroll the view.
From PDFKit
I've tried:
previewView.layoutDocumentView()
for pageIndex in 0...pdf.pageCount - 1 {
let page = pdf.page(at: pageIndex)!
previewView.annotationsChanged(on: page)
}
and from NSView
I've tried:
previewView.needsDisplay = true
previewView.needsLayout = true
previewView.documentView?.needsDisplay = true
previewView.updateLayer()
but no luck. I've tried scrolling the PDFView
with code too but it hasn't been a reliable way of sneaking a refresh, and in general shouldn't be the way to do this.
来源:https://stackoverflow.com/questions/39868300/pdfview-setneedsdisplayyes-doesnt-work-on-macos-sierra-10-12