0

我正在创建一个基本的绘图应用程序;我认为一切都很好并且可以正常工作,直到我在实际设备上对其进行了测试。在 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;




}}
4

1 回答 1

0

我实际上在这里找到了解决方案:CoreGraphics 绘图导致 iOS 7 上的内存警告/崩溃

修复涉及使用 autoreleasepool{} 来释放内存,显然这只发生在 iOS7 上。这是修改后的代码:

-(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;

    }




}}
于 2014-08-07T22:03:51.007 回答