【问题标题】:"Potential leak of an object" when using Core Graphics使用 Core Graphics 时“对象的潜在泄漏”
【发布时间】:2016-01-15 23:02:58
【问题描述】:

我的应用正在使用以下代码绘制阴影:

-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{

    CGContextSaveGState(context);

    //Set color of current context
    [[UIColor blackColor] set];

    //Set shadow and color of shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);

    CGContextFillEllipseInRect(context, rect);

    CGContextClipToMask(context, rect, CGBitmapContextCreateImage(context));

    CGContextRestoreGState(context); // Warning shows in this line

}

当我运行产品 > 分析时,它会用以下消息标记此块的最后一条指令:“对象可能泄漏”。当我删除该行时,它会显示相同的消息,但在右括号中。

知道我做错了什么吗?

谢谢

【问题讨论】:

    标签: ios objective-c core-graphics


    【解决方案1】:

    我认为泄漏的是CGBitmapContextCreateImage

    尝试将其分配给一个局部变量,然后在剪辑到掩码后使用它调用CGImageRelease

    Another iPhone - CGBitmapContextCreateImage Leak

    【讨论】:

      【解决方案2】:

      chedabob 的回答有效!这是最终代码:

      -(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
          CGContextSaveGState(context);
          //Set color of current context
          [[UIColor blackColor] set];
          //Set shadow and color of shadow
          CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
          CGContextFillEllipseInRect(context, rect);
      
          CGImageRef bitmap = CGBitmapContextCreateImage(context);
          CGContextClipToMask(context, rect, bitmap);
          CGContextRestoreGState(context);
      
          CGImageRelease(bitmap);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-25
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多