CGlayer的内存问题 [英] Memory issues with CGlayer

查看:135
本文介绍了CGlayer的内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CGlayers进行绘图.我已经实现了绘图部分,用户将在其中绘制的drawingView(canvas)是动态的,我的意思是,用户可以增加/减小drawingView(Canvas)的高度

I am working CGlayers for drawing. I have implemented the drawing part, the drawingView(canvas) where the user will draw is dynamic, what I mean by this is, user can increase/decrease height of the drawingView(Canvas)

例如默认大小-500 * 200

For example by default size - 500*200

当用户单击展开按钮时-500 * 300

When user clicks on expand button - 500*300

这是用户展开画布时的功能,

So here is my function when user expands the canvas,

- (void)IncreaseCanavasSize
{
    CGContextRef layerContext1 = CGLayerGetContext(permanentDrawingLayer );
    CGContextDrawLayerInRect(layerContext1, rectSize, newDrawingLayer);

    rectSize = self.bounds;

    CGFloat scale = self.contentScaleFactor;
    CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
    CGLayerRef layer = CGLayerCreateWithContext(layerContext1, bounds.size, NULL);
    CGContextRef layerContext = CGLayerGetContext(layer);
    CGContextScaleCTM(layerContext, scale, scale);
    [self setNewDrawingLayer:layer];
    CGLayerRelease(layer);         

   CGContextDrawLayerInRect(layerContext, self.bounds, permanentDrawingLayer);
        permanentDrawingLayer = nil;
}

所以,让我解释一下上面代码中的操作,我正在创建一个尺寸更大的newLayer,然后再将其从"* permanentDrawingLayer "转移到" newDrawingLayer "并制作"permanentDrawing" *

So let me explain what I am doing in the above code, I am creating a newLayer with size before it is increased, and then transfer the previousDrawing from "*permanentDrawingLayer" to this "newDrawingLayer" and making "permanentDrawing" nil*

所以每当我绘制时,我都会绘制到永久绘图层中,这是我的drawRect方法

So Whenever I draw, I draw into permanentDrawingLayer, here is my drawRect method

- (void)drawRect:(CGRect)rect
{

   if(permanentDrawingLayer == 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 setPermanentDrawingLayer:layer];
         CGLayerRelease(layer);
   }


   CGContextRef layerContext = CGLayerGetContext(permanentDrawingLayer);
   CGContextBeginPath(layerContext);
   CGContextAddPath(layerContext, mutablePath);
   CGContextSetLineWidth(layerContext, self.lineWidth);
   CGContextSetLineCap(layerContext, kCGLineCapRound);
   CGContextSetLineJoin(layerContext, kCGLineJoinRound);
   CGContextSetAllowsAntialiasing(layerContext, YES);
   CGContextSetShouldAntialias(layerContext, YES);
   CGContextSetStrokeColorWithColor(layerContext, self.lineColor.CGColor);
   CGContextSetFillColorWithColor(layerContext, self.lineColor.CGColor);
   CGContextSetBlendMode(layerContext,kCGBlendModeNormal);
   CGContextStrokePath(layerContext);

   CGContextDrawLayerInRect(context,rectSize, newDrawingLayer);
   CGContextDrawLayerInRect(context, self.bounds, permanentDrawingLayer);
}

在这里您可以看到,我使用rectSize绘制newDrawingLayer,使用newSize绘制permanetDrawingLayer,因此,每当我在画布上绘制时,如果用户增加了尺寸,newDrawingLayer都会绘制它,并且无论用户执行什么操作都将绘制新图在永久绘图层中.希望一切都清楚.

Here you can see, I draw newDrawingLayer with rectSize, and permanetDrawingLayer with newSize, So whenever, I draw on a canvas, if user has increased the size, the newDrawingLayer will draw that, and new drawing whatever user does will be done in permanentDrawingLayer. Hope it is clear.

现在这是我的问题

1)当我drawSomething并增加canvasSize时,内存峰值达到10MB,因此如果您始终可以执行此操作,您可以想象,由于内存压力,我的应用程序终止的速度有多快.

1) Memory spikes to 10MB, when I drawSomething and increase the canvasSize, so if I do this action always you can imagine, how fast my app will terminate due to memory pressure.

2)我看到的是,如果我在函数-" IncreaseCanavasSize "中注释了行" permanentDrawingLayer = nil ",那么内存不会增加,但是如果我不这样做这样做,那么当我下次绘制时,将不会创建具有newSize的图层,并且将获得重复的图纸.

2) What I saw was if I comment the Line " permanentDrawingLayer = nil" in function - "IncreaseCanavasSize", then memory doesnt spikes up, but if I dont do that, then when I draw next time, then Layer with newSize will not be created and I will get duplicate drawings.

所以我需要你的所有帮助

So I need all your help

推荐答案

您的permanentDrawingLayerCGLayerRef,因此将其设置为NULL时不要释放它. 您需要先调用CGLayerRelease(permanentDrawingLayer),然后再将其设置为NULL.

Your permanentDrawingLayer is a CGLayerRef so setting it to NULL, don't release it. You need to call CGLayerRelease(permanentDrawingLayer) before setting it to NULL.

- (void)IncreaseCanavasSize
{
     CGContextRef layerContext1 = CGLayerGetContext(permanentDrawingLayer );
     CGContextDrawLayerInRect(layerContext1, rectSize, newDrawingLayer);

     rectSize = self.bounds;

     CGFloat scale = self.contentScaleFactor;
     CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
     CGLayerRef layer = CGLayerCreateWithContext(layerContext1, bounds.size, NULL);
     CGContextRef layerContext = CGLayerGetContext(layer);
     CGContextScaleCTM(layerContext, scale, scale);
    [self setNewDrawingLayer:layer];
     CGLayerRelease(layer);         

     CGContextDrawLayerInRect(layerContext, self.bounds, permanentDrawingLayer);

    CGLayerRelease(permanentDrawingLayer);
    permanentDrawingLayer = NULL;
}

如果您的方法-setPermanentDrawingLayer:看起来像上一个问题中的方法-setCurrentDrawingLayer:. 在-IncreaseCanavasSize中,您可以简单地将permanentDrawingLayer = nil;行替换为[self setPermanentDrawingLayer:NULL];,然后它将释放CGLayerRef并将其设置为NULL.

Also if your method -setPermanentDrawingLayer: looks like the method -setCurrentDrawingLayer: in your previous question. In -IncreaseCanavasSize You can simply replace the line permanentDrawingLayer = nil; with [self setPermanentDrawingLayer:NULL];, then it will release the CGLayerRef and set it to NULL.

这篇关于CGlayer的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆