iPad绘图应用程序的内存压力问题 - 问题仅出现在实际的iPad上 - 而不是模拟器上

时间:2014-08-07 17:32:50

标签: ios xcode ipad ios-simulator memory-pressure

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




}}

1 个答案:

答案 0 :(得分:0)

我实际上在这里找到了解决方案:CoreGraphics drawing causes memory warnings/crash on 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;

    }




}}