CoreGraphics绘图会在iOS 7上导致内存警告/崩溃 [英] CoreGraphics drawing causes memory warnings/crash on iOS 7

查看:628
本文介绍了CoreGraphics绘图会在iOS 7上导致内存警告/崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的iPad(mini)更新到iOS7之后我经历过我的绘图应用程序在几次中风后滞后并崩溃。

After updating my iPad (mini) to iOS7 I experienced that my drawing app is lagging and crashing after a few strokes.

现在,当我运行应用程序时xcode 5中的仪器/内存分配工具,我看到在屏幕上绘图时, VM:CG栅格数据类别正在迅速填满。似乎有很多 CGDataProviderCreateWithCopyOfData 调用,每个调用大小为3.00Mb。连续绘制后,应用程序会收到内存警告,并且通常会终止。

Now, when I run the app with Instruments/memory allocation tool in xcode 5, I see that the VM: CG raster data category is filling up rapidly when drawing on the screen. There seems to be a multitude of CGDataProviderCreateWithCopyOfData calls being made, each 3.00Mb in size. After continous drawing the app receives memory warnings, and usually terminates.

代码基本上会将路径转换为imagecontext,或多或少会像这样:

The code basically strokes paths into an imagecontext, more or less like this:

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();

在iOS7 / iPad上,这是非常滞后的并且存在内存问题,而在iOS6上这是相当快速的没有负面的内存占用。

On iOS7/iPad this is extremely laggy and has memory issues, while on iOS6 this was fairly speedy and had no negative memory footprint.

当我在非视网膜iPhone版本中运行此代码时, CGDataProviderCreateWithCopyOfData 调用的大小为604Kb,且仅限一两个人同时活跃。绘图流畅而快速,没有内存警告且没有减速。

When I run this code in a non-retina iPhone version the CGDataProviderCreateWithCopyOfData calls are 604Kb in size, and only one or two are "active" at the same time. The drawing is fluent and fast with no memory warnings and no slowdown.

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

What happened from iOS6 to iOS7 regarding CoreGraphics and imagecontexts?

对于任何语言错误或其他可能的愚蠢错误,我们深表歉意。还是一个菜鸟,在我的业余时间做iOS开发。

Sorry for any lingo errors or other possible stupid mistakes. Still a noob, doing iOS development in my spare time.

推荐答案

我把我的绘图代码放在autoreleasepool中。这解决了我的问题问题。

I put my drawing code in an autoreleasepool .and this solved my problem.

例如: -

@autoreleasepool {
    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();
}

这篇关于CoreGraphics绘图会在iOS 7上导致内存警告/崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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