我正在开发一个绘图应用程序,我正在绘制CGlayers,我正在以这种方式创建一个 CGlayer
-(void)drawRect
{
if(self.DrawingLayer == nil)
{
CGFloat scale = self.contentScaleFactor;
CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextScaleCTM(layerContext, scale, scale);
self.DrawingLayer = layer;
}
CGContextDrawLayerInRect(context,rectSize, self.newDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.DrawingLayer );
}
现在,由于我的绘图 Canvas 可以动态增加/减少,每当用户执行任何操作时,我都会再创建一个图层并将以前的绘图存储到新的 CGlayer 上
-(void)canvasIncreased
{
rectSize = self.bounds
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scale = self.contentScaleFactor;
CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextScaleCTM(layerContext, scale, scale);
self.newDrawingLayer = layer;
CGContextDrawLayerInRect(layerContext, self.bounds, self.DrawingLayer);
self.DrawingLayer = nil;
}
一切正常,但发生的情况是,每当我画一些东西然后增加画布大小,再次绘制,然后增加画布大小,然后再次绘制等等,
然后,我之前的绘图变得像素化,这是图像