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];
}
感謝所有幫助。
它看起來像當你創建UIImageView(drawImage),然後將其傳遞給子視圖時,有一個內存泄漏。您正在創建大量的UIImageView,而無需發佈任何代碼 - 無論如何您都已顯示代碼。 –
您是否使用過儀器來追蹤內存問題? – rmaddy