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 drawing cause 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;

    }




}}
于 2014-08-07T22:03:51.007 に答える