4

我曾经[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 来画一条线。

4

1 回答 1

0

我有同样的麻烦。第一次添加注释时,PDFView会立即在页面上显示该注释。从那时起,添加或删除注释在代码中可以正常工作,但PDFView在我手动滚动视图之前不会显示更改。

PDFKit我尝试过:

previewView.layoutDocumentView()

for pageIndex in 0...pdf.pageCount - 1 {
  let page = pdf.page(at: pageIndex)!
  previewView.annotationsChanged(on: page)
}

NSView我尝试过:

previewView.needsDisplay = true
previewView.needsLayout = true
previewView.documentView?.needsDisplay = true
previewView.updateLayer()

但没有运气。我也尝试过PDFView使用代码滚动,但它并不是一种可靠的刷新刷新方式,而且通常不应该是这样做的方式。

于 2016-10-06T13:10:10.390 回答