Reputation: 481
I'm creating a basic drawing app; I thought everything was fine and working until I tested it on an actual device. When used on an iPad mini I am getting a memory pressure crash while drawing, on the simulator I have no such issue. It is bizarre, because I can watch the memory usage skyrocket while drawing on an actual device but the same phenomenon cannot be reproduced on the simulator. Below I've posted the code I'm using to draw. Do you see anything here that would be causing this? How could I reduce memory usage? Thank you.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
mouseSwiped = YES;
if ([eraserButtonStatus isEqual: @"OFF"]) {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView: self.mainImage];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
// CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(),NO);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), thickness );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red,green,blue, thickness);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
else{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView: self.mainImage];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(),NO);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), thickness );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red,green,blue, thickness);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}}
Upvotes: 0
Views: 158
Reputation: 481
I actually found the solution here: CoreGraphics drawing causes memory warnings/crash on iOS 7
The fix involved using autoreleasepool{} to free up memory, apparently this only happens on iOS7. Here is the modified code:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
mouseSwiped = YES;
if ([eraserButtonStatus isEqual: @"OFF"]) {
@autoreleasepool {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView: self.mainImage];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
// CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(),NO);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), thickness );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red,green,blue, thickness);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
}
else{
@autoreleasepool {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView: self.mainImage];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(),NO);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), thickness );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red,green,blue, thickness);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
}}
Upvotes: 0