首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreGraphics绘图会导致iOS 7上的内存警告/崩溃

CoreGraphics绘图会导致iOS 7上的内存警告/崩溃
EN

Stack Overflow用户
提问于 2013-10-03 19:33:57
回答 2查看 6.6K关注 0票数 10

在将我的iPad (mini)升级到iOS7之后,我感觉到我的绘图应用程序在几次笔画后出现了滞后和崩溃。

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

代码基本上将路径笔画到图像文本中,大致如下:

代码语言:javascript
复制
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();

在iOS6 7/iPad上,这是非常滞后的,存在内存问题,而在iOS6上,这是相当迅速的,没有内存占用。

当我在非视网膜CGDataProviderCreateWithCopyOfData版本中运行这段代码时,iPhone调用的大小是604Kb,同时只有一两个是“活动的”。绘图流畅快捷,没有内存警告,也没有减速。

从iOS6到iOS7,关于CoreGraphics和图像文本发生了什么?

对任何语言错误或其他可能的愚蠢错误表示歉意。仍然是个菜鸟,在业余时间做iOS开发。

EN

回答 2

Stack Overflow用户

发布于 2013-12-02 06:31:06

我把我的绘图代码放在一个自动发布池.and中,这解决了我的问题。

例:-

代码语言:javascript
复制
@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();
}
票数 11
EN

Stack Overflow用户

发布于 2013-11-04 15:47:48

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

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

代码语言:javascript
复制
- (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();
}

在touchesMoved上,我调用一个drawLine方法,它做一些曲线插值,画笔尺寸调整和结束线变细,然后做一个自我setNeedsDisplay;

这在iOS7中似乎运行得很好。对不起,如果我不能更具体,但我宁愿不发布实际的生产代码从我的应用程序:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19167732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档