我正在创建一个基本的绘图应用程序;我认为一切都很好并且可以正常工作,直到我在实际设备上对其进行了测试。在 iPad mini 上使用时,我在绘图时遇到内存压力崩溃,在模拟器上我没有这样的问题。这很奇怪,因为我可以在实际设备上绘图时看到内存使用量猛增,但在模拟器上无法重现相同的现象。下面我发布了我用来绘制的代码。你看到这里有什么会导致这种情况的吗?我怎样才能减少内存使用?谢谢你。
-(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;
}}