由于内存分配,iphone应用程序崩溃了

时间:2014-01-15 16:45:42

标签: ios memory uiimageview motion-detection

我正在编写一个在视图中绘制内容的应用程序,它基于devicemotion绘制。但是大约4分钟后应用程序崩溃了,我收到了这个警告:

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];
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题在于:

[self.view addSubview:drawImage];

您可以在另一个之后添加一个子视图,而无需删除一个子视图。注意删除/释放未使用的子视图,您的记忆问题将消失。