Coregraphics 中的内存管理 (iOS)

2023-12-15

我正在开发一个绘图应用程序,我使用 CGlayers 进行绘图,因此我通过单击按钮打开画布进行绘图,

我正在使用UIBezierPath,然后将其转换为下面的touchesMoved中的CGPath,然后用它来绘制

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {           
        if (ctr == 4)
        {
            m_touchMoved = true;

            self.currentPath = [[DrawingPath alloc] init];

            [self.currentPath setPathColor:self.lineColor];
            self.currentPath.pathWidth = [NSString stringWithFormat:@"%f",self.lineWidth];


             pts[3] = midPoint(pts[2], pts[4]);// move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
            [self.currentPath.path moveToPoint:pts[0]];
            [self.currentPath.path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];

            CGPathRef cgPath = self.currentPath.path.CGPath;
            mutablePath = CGPathCreateMutableCopy(cgPath);        

            [self setNeedsDisplay];

            pts[0] = pts[3];
            pts[1] = pts[4];
            ctr = 1;
        }

    }

这是我的drawRect方法

- (void)drawRect:(CGRect)rect
    {
       switch (m_drawStep)
       {
           case DRAW:
           {

               CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw)


               if(currentDrawingLayer == 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);
                   //currentDrawingLayer = layer;
                   [self setCurrentDrawingLayer:layer];
                   CGLayerRelease(currentDrawingLayer);
               }




               CGContextRef layerContext = CGLayerGetContext(currentDrawingLayer);
               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);
               CGPathRelease(mutablePath);

              CGContextDrawLayerInRect(context, self.bounds, currentDrawingLayer );
           }
               break;

    }

我已经手动创建了 setter 方法

-(void)setCurrentDrawingLayer:(CGLayerRef)layer
{
    CGLayerRetain(layer);
    CGLayerRelease(currentDrawingLayer);
    currentDrawingLayer = layer;
}

现在,当用户单击保存按钮时,我可以通过这种方式从画布中获取图像

-(void)getImageFromCanvas
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO,0.0);//Creates a bitmap based graphics context with
                                                                         //specified options
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];//Renders the reciever and its layers into specified
                                                                   //context.
        m_curImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();//Removes the current context from top of stack

     if(currentDrawingLayer)
     {
            CGLayerRelease(currentDrawingLayer);
     }

  [m_delegate performSelectorOnMainThread:@selector(getCanVasViewImage:)
                                 withObject:m_curImage
                              waitUntilDone:NO];


}

我获取图像并将其显示在屏幕上的网格上,并调整了图像大小,但是当我执行这些操作时,我的内存使用量总是激增,并且没有减少,这是创建三个绘图后的屏幕截图

要调整图像大小,我使用它,我在单独的线程中运行它

dispatch_async(dispatch_get_global_queue(0,0), ^{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

     [image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];


     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();
});

enter image description here

内存持续增加,最终在创建了大约 30 个绘图后,我的应用程序崩溃了,因为内存飙升至 550MB

所以,我不明白我哪里出了问题,如何在绘图时管理内存问题。


Your mutablePath泄漏,因为你释放了前一个-drawRect:(并且仅当m_drawStep == DRAW). As -setNeedDisplay只是将视图标记为需要重绘。-drawRect:只在下一个绘图周期发送,所以几个-touchesMoved:withEvent:可以在绘图周期之间附加。 还有一些泄漏,当m_drawStep != DRAW.

所以删除CGPathRelease(mutablePath) from -drawRect:方法并释放它-touchesMoved:withEvent:

CGPathRef cgPath = self.currentPath.path.CGPath;
CGPathRelease(mutablePath);
mutablePath = CGPathCreateMutableCopy(cgPath);        

[self setNeedsDisplay];

同时释放CGLayerRef设置后创建-setCurrentDrawingLayer:因为你将它保留在-setCurrentDrawingLayer:.

if(currentDrawingLayer == nil)
{
    // ...
    CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL); // layer is created, refCount == 1
    // ...
    [self setCurrentDrawingLayer:layer]; // layer is retained by -setCurrentDrawingLayer:, refCount == 2
     CGLayerRelease(layer); // release layer, refCount == 1

     // without it the previous release layer refCount still == 2, so in -setCurrentDrawingLayer:
     // CGLayerRelease(currentDrawingLayer) decrement refCount to 1 and leak...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Coregraphics 中的内存管理 (iOS) 的相关文章

随机推荐