Ipad CoreGraphics绘图在iOS 7上导致内存警告/崩溃

Ipad CoreGraphics绘图在iOS 7上导致内存警告/崩溃,ipad,uiimage,core-graphics,ios7,Ipad,Uiimage,Core Graphics,Ios7,在将我的iPad(mini)升级到iOS7后,我感觉我的绘图应用程序在几下之后就落后了,崩溃了 现在,当我在xcode 5中使用Instruments/memory allocation tool运行应用程序时,我看到当在屏幕上绘图时,VM:CG光栅数据类正在快速填充。似乎有大量的CGDataProviderCreateWithCopyOfData调用正在进行,每个调用的大小为3.00Mb。连续绘图后,应用程序会收到内存警告,通常会终止 代码基本上将路径划入imagecontext,大致如下所示

在将我的iPad(mini)升级到iOS7后,我感觉我的绘图应用程序在几下之后就落后了,崩溃了

现在,当我在xcode 5中使用Instruments/memory allocation tool运行应用程序时,我看到当在屏幕上绘图时,VM:CG光栅数据类正在快速填充。似乎有大量的CGDataProviderCreateWithCopyOfData调用正在进行,每个调用的大小为3.00Mb。连续绘图后,应用程序会收到内存警告,通常会终止

代码基本上将路径划入imagecontext,大致如下所示:

UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGImageRef cacheImage = CGBitmapContextCreateImage(cacheContext);
    CGContextDrawImage(context, self.bounds, cacheImage);

    // Combine cache with image
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(cacheImage);
    UIGraphicsEndImageContext();
}
在iOS7/iPad上,这是非常滞后的,并且有内存问题,而在iOS6上,这是相当快的,并且没有负内存占用

当我在非视网膜iPhone版本中运行此代码时,CGDataProviderCreateWithCopyOfData调用的大小为604Kb,并且只有一两个同时处于“活动”状态。绘图流畅、快速,没有内存警告和减速

关于CoreGraphics和ImageContext,从iOS6到iOS7发生了什么


对于任何行话错误或其他可能的愚蠢错误,我深表歉意。我还是个新手,在业余时间做iOS开发。

我的解决方案通常是创建一个Canvas UIView类来处理所有绘图操作。我写入缓存的CGImageRef,然后将缓存与UIImage按照以下方式组合:

我的自定义drawRect方法如下所示:

UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGImageRef cacheImage = CGBitmapContextCreateImage(cacheContext);
    CGContextDrawImage(context, self.bounds, cacheImage);

    // Combine cache with image
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(cacheImage);
    UIGraphicsEndImageContext();
}
在TouchsMoved上,我调用了一种drawLine方法,该方法可以进行一些曲线插值、笔刷大小调整和端点变细,然后执行[self-setNeedsDisplay]


这似乎在iOS7中运行良好。抱歉,我不能说得更具体一些,但我不想发布我应用程序中的实际生产代码:)

我将绘图代码放入自动删除池中。这解决了我的问题

例如:-


嗨,你找到什么可能的解决办法了吗?我也遇到了同样的问题。还没有,但我正在研究将绘图代码移到UIView的drawRect方法中(这对于我的特殊需求还有其他缺点)。如果找到解决方案,将发布更新。ACEDrawingView是第三方库,在iOS7和iOS6中都具有良好的性能。到目前为止,我还没有弄清楚我们的和这个的区别。也许你可以从这个+uxCode 5仪器中找到一些夸大内存问题的东西。当我使用仪器时,我的应用程序会在30秒内崩溃,否则它运行正常,尽管当我进行长时间的连续绘制笔划时它会使应用程序陷入困境(我也有一个放大镜或放大视图)。嗨,我也遇到了同样的问题。你找到了解决方案吗,因为ARC也没有任何帮助…哇,我在iPad Mini上遇到了完全相同的问题,这解决了这个问题!太奇怪了,因为问题从来没有在模拟器上发生过。