2014-01-15 48 views
0

我正在编写一个应用程序,该应用程序在视图中绘制某些内容,并基于devicemotion进行绘制。但4分钟使应用程序崩溃后,我得到这样的警告:由于内存分配,iphone应用程序崩溃

2014-01-15 17:04:16.605 appName[3427:60b] Received memory warning. 

这是我的代码有:

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    //FIXME: App crash due Memory allocations 
     self.motionManager =[[CMMotionManager alloc] init]; 
    self.motionManager.deviceMotionUpdateInterval = .2; 
    [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] 
withHandler:^(CMDeviceMotion *motion, NSError *error) { 
     [self motionMethod:motion]; 
    }]; 
} 

-(void)motionMethod:(CMDeviceMotion *)deviceMotion 
{ 
    drawImage.image = nil; 
    drawImage = [[UIImageView alloc] init]; 
    drawImage.frame = self.view.frame; 

    [self.view addSubview:drawImage]; 
    float accelerationThreshold = 0.005; 
    CMAcceleration userAcceleration = deviceMotion.userAcceleration; 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsBeginImageContext(CGSizeMake(320, 568)); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.5); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 

    float diff = fabs((userAcceleration.x + userAcceleration.y + userAcceleration.z)-oldMove); 

    if (fabs(userAcceleration.x) < accelerationThreshold || 
     fabs(userAcceleration.y) < accelerationThreshold || 
     fabs(userAcceleration.z) < accelerationThreshold) 
    { 
     //vertical top 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 , 
          self.view.bounds.size.height/2 + ((self.view.bounds.size.height/100)*2.5)); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width /2, 
           self.view.bounds.size.height -((self.view.bounds.size.height/100)*30)); 
     //vertical down 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 , 
          self.view.bounds.size.height/2 - ((self.view.bounds.size.height /100) *2.5)); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width /2, 
           ((self.view.bounds.size.height/100)*30)); 

     //horizontal right 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 + ((self.view.bounds.size.width/100)*5) , 
          self.view.bounds.size.height/2); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width - ((self.view.bounds.size.width/100)*20) , 
           self.view.bounds.size.height /2 ); 
     //horizontal left 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 - ((self.view.bounds.size.width/100)*5) , 
          self.view.bounds.size.height/2); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), ((self.view.bounds.size.width /100)*20), 
           self.view.bounds.size.height /2 ); 
    } 
    else 
    { 
     float pos = 300 * diff; 

     int radius = 100; 
     int cX = self.view.bounds.size.width/2 -50; 
     int cY = self.view.bounds.size.height /2 -50; 

     CGRect borderRect1 = CGRectMake(cX - pos, cY -pos, radius, radius); 
     CGRect borderRect2 = CGRectMake(cX + pos, cY -pos, radius, radius); 
     CGRect borderRect3 = CGRectMake(cX , cY + pos, radius, radius); 

     CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect1); 
     CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect2); 
     CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect3); 

    } 

    oldMove = accelerationThreshold; 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    } 

能有人帮我解决这个问题?非常感谢。

编辑: 这是我没有内存泄漏的新方法:

-(void)motionMethod:(CMDeviceMotion *)deviceMotion 
{ 
    drawImage.image = nil; 
    if (drawImage.image == nil) { 
     [drawImage removeFromSuperview]; 
    } 
drawImage = [[UIImageView alloc] init]; 
drawImage.frame = self.view.frame; 
float accelerationThreshold = 0.002; 
CMAcceleration userAcceleration = deviceMotion.userAcceleration; 

UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.5); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 

float diff = fabs((userAcceleration.x + userAcceleration.y + userAcceleration.z)-oldMove); 

if (fabs(userAcceleration.x) < accelerationThreshold || 
    fabs(userAcceleration.y) < accelerationThreshold || 
    fabs(userAcceleration.z) < accelerationThreshold) 
{ 
    //vertical top 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 , 
         self.view.bounds.size.height/2 + ((self.view.bounds.size.height/100)*2.5)); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width /2, 
          self.view.bounds.size.height -((self.view.bounds.size.height/100)*30)); 
    //vertical down 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 , 
         self.view.bounds.size.height/2 - ((self.view.bounds.size.height /100) *2.5)); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width /2, 
          ((self.view.bounds.size.height/100)*30)); 

    //horizontal right 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 + ((self.view.bounds.size.width/100)*5) , 
         self.view.bounds.size.height/2); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width - ((self.view.bounds.size.width/100)*20) , 
          self.view.bounds.size.height /2 ); 
    //horizontal left 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width/2 - ((self.view.bounds.size.width/100)*5) , 
         self.view.bounds.size.height/2); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), ((self.view.bounds.size.width /100)*20), 
          self.view.bounds.size.height /2 ); 
} 
else 
{ 
    float pos = 300 * diff; 

    int radius = 100; 
    int cX = self.view.bounds.size.width/2 -50; 
    int cY = self.view.bounds.size.height /2 -50; 

    CGRect borderRect1 = CGRectMake(cX - pos, cY -pos, radius, radius); 
    CGRect borderRect2 = CGRectMake(cX + pos, cY -pos, radius, radius); 
    CGRect borderRect3 = CGRectMake(cX , cY + pos, radius, radius); 

    CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect1); 
    CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect2); 
    CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), borderRect3); 

} 

oldMove = accelerationThreshold; 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
[drawImage setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[self.view addSubview:drawImage]; 
} 

感谢所有帮助。

+0

它看起来像当你创建UIImageView(drawImage),然后将其传递给子视图时,有一个内存泄漏。您正在创建大量的UIImageView,而无需发布任何代码 - 无论如何您都已显示代码。 –

+0

您是否使用过仪器来追踪内存问题? – rmaddy

回答

1

的问题是在这里:

[self.view addSubview:drawImage]; 

你一后添加一个子视图而没有删除一个。照顾删除/释放未使用的子视图,你的记忆问题将消失。