我使用[PDFView setNeedsDisplay:YES]
让PDF视图重绘,它在OSX 10.9-10.11上工作得很好。然而,它不工作,除非我放大或缩小PDF页面…
是否有其他方法可以立即重画?下列代码:
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;
:
我发现setNeedsDispaly
调用 drawPage:toContext:
然而,绘图代码在 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];
}
调试说CGContextSetFillColorWithColor: invalid context 0x0
和更多的invalid context 0x0
警告。我在drawPage:toContext:
中所做的是测试,只是使用BezierPath画一条线。
我也有同样的问题。我第一次添加注释时,PDFView
立即在页面上显示该注释。从那时起,添加或删除注释在代码中工作良好,但PDFView
不显示更改,直到我手动滚动视图。
From PDFKit
I've try:
previewView.layoutDocumentView()
for pageIndex in 0...pdf.pageCount - 1 {
let page = pdf.page(at: pageIndex)!
previewView.annotationsChanged(on: page)
}
和from NSView
I've try:
previewView.needsDisplay = true
previewView.needsLayout = true
previewView.documentView?.needsDisplay = true
previewView.updateLayer()
但运气不好。我也试过用代码滚动PDFView
,但它不是一种可靠的刷新方式,一般情况下不应该这样做。