IOS,如何清除上下文图形

lop*_*710 5 iphone objective-c ipad cgcontextdrawpdfpage

在我的应用程序中,我有一个方法将pdf绘制到上下文中:

 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);

 CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFTrimBox),
                                                          CGContextGetClipBoundingBox(ctx));

 CGContextConcatCTM(ctx, transform);

 CGContextDrawPDFPage(ctx, page);
Run Code Online (Sandbox Code Playgroud)

现在在drawLayer中,在缩放时调用,我进行必要的转换并再次调用 CGContextDrawPDFPage(ctx, page);

会发生的是,在第一个pdf之上绘制缩放的pdf,问题是在仅包含文本的特定页面中,显示了背面和模糊的pdf.这很奇怪,我认为pdf页面有白色背景,如果发生这种情况,那是因为顶部的缩放pdf具有透明背景.

现在,为了解决这个问题,如何在drawLayer方法的CGContextDrawPDFPage(ctx,page)之前清除上下文?我试过了:

//self.view.transform = CGAffineTransformIdentity;

//CGAffineTransform transform = CGAffineTransformIdentity;
//CGContextConcatCTM(ctx, transform);

//CGContextClearRect(ctx, layer.bounds);
Run Code Online (Sandbox Code Playgroud)

什么都行不动......提前谢谢

Wil*_*ill 12

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
Run Code Online (Sandbox Code Playgroud)


coc*_*ali 4

您是否尝试按如下方式刷新上下文?

CGContextFlush(ctx);
Run Code Online (Sandbox Code Playgroud)