我用
[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;
更新:
我发现setNeedsDisplay
调用了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来绘制一条线。
drawPage:toContext:
或PDFPage的drawWithBox:toContext:
中,Cocoa绘图方法不起作用,因为它们在不同的线程上调用,该线程没有设置NSGraphicsContext。您需要显式地使用传递的上下文。 - danielv