Проблема нехватки памяти в приложении для рисования iPad - проблема появляется только на реальном iPad, а не на симуляторе
Я создаю базовое приложение для рисования; Я думал, что все было хорошо и работает, пока я не проверил это на реальном устройстве. При использовании на 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 ответ
Я на самом деле нашел решение здесь: 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;
}
}}