• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - iPad上的drawRect导致内存问题

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:57:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 5-tab Tab Bar Controller iPad 应用程序。 选项卡之一 (EKG) 导致内存问题。我已经运行了 Instruments,我所能看到的只是 malloc 分配不断增加,大约 12 分钟后,我的所有 View Controller 首先获得 didReceiveMemoryWarning 级别 1,然后是2 级,然后是 SigAbort 0 终止

应用程序的设计工作方式是,当 EKG 选项卡处于事件状态时,每 200 毫秒触发一次 setNeedsDisplay,以便在屏幕上绘制(绘制)EKG 样本。当我让应用程序正常运行时,它会在大约 12 分钟后终止。

但是,如果我保留 setNeedsDisplay 并注释掉 drawRect 中的代码,它将运行 永远。我不知道我的“drawRect”中有任何内存分配,但有人在做这些 mallocs 下面是我的 drawRect 代码:

- (void) drawRect : (CGRect) rect 
{
    int i, ii, x = 0, xx, y;        

    fEcgDraw = YES;     
    CGContextRef context = UIGraphicsGetCurrentContext ();
    CGContextSetLineWidth (context, 1);             
    HH = 376;                               
    if (fEcgErase == YES)                           
    {
        CGContextSetStrokeColorWithColor (context,      
                     [UIColor blackColor].CGColor);
/*==============================================================================*/
/* Erase the last screen.                                                       */
/*==============================================================================*/
        for (i = 0; i < 120; i++)                   
        {        
            CGContextMoveToPoint (context,          
                          ECGX[x],
                          (HH - ECGS[x]));      
            CGContextAddLineToPoint (context, ECGX[(x + 1)],    
                         (HH - ECGS[((x + 1) % 119)])); 
            x++;                            
        }   // end - for (i = 0; i < 120; i++)
        CGContextStrokePath (context);          
        fEcgErase = NO;             
    }   // end - if (fEcgErase == YES)
    else if (fECGLOOP)                  
    {
        xx = 1;             
        x = 0;                  
        y = YY;                         
        ii = 0;             
        for (i =
        { 
//          if (xx == 1)                
            {
/*==============================================================================*/
/* First erase the prior ECG A/D plot value.                                    */
/*==============================================================================*/
                CGContextSetStrokeColorWithColor (context,  
                        [UIColor blackColor].CGColor);  
                CGContextMoveToPoint (context,          
                            ECGX[x],    
                            (HH - ECGS[x]));
                CGContextAddLineToPoint (context,       
                            ECGX[(x + 1)],      
                            (HH - ECGS[((x + 1))]));
                CGContextStrokePath (context);          
/*==============================================================================*/
/* Now plot the next ECG A/D plot value.                                        */
/*==============================================================================*/
                CGContextSetStrokeColorWithColor (context,  
                        [UIColor whiteColor].CGColor);  
                CGContextMoveToPoint (context,      
                              ECGX[x],      
                              (HH - ECGY[y]));  
                CGContextAddLineToPoint (context,   
                             ECGX[(x + 1)],     
                             (HH - ECGY[((y + 1) % 119)])); 
                CGContextStrokePath (context);          
                ECGS[x] = ECGY[y];      
                x++;                
                y = ((y + 1) % 119);        
            }   // end - if (xx == 1)
        }       // end - for (i = 0; i < 120; i++)
        y = ((y + count1) % 119);               
        YY = y;                         
        count1 = 0;                 
    }   // end - if (fEcgErase == YES)
    fEcgDraw = NO;                              

}   // end - 'drawRect'
/*===============================END OF FUNCTION================================*/



Best Answer-推荐答案


问题已通过在每个“CGContextStrokePath”之前添加“CGContextClosePath”得到解决,应用现在将永远运行而没有任何“didReceiveMemoryWarning”。

-吉尔·古德里奇

关于ios - iPad上的drawRect导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216165/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap