Как избежать утечки памяти?

Собственно, в ImageView ViewController'a, пользователь может рисовать линии и элипсы. В методе touchesMoved постоянно создается куча image, которые забивают оперативку. Как этого избежать?

@interface ViewController ()

@end

@implementation ViewController

@synthesize drawImage;
@synthesize segments;



- (void)viewDidLoad
{
    [super viewDidLoad];
	
    
    [Singleton sharedMySingleton].arrayMain=[[NSMutableArray alloc] init];
    
   
    
}








- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    
    
   
   
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
#define RAND_FROM_TO(min, max) (min + arc4random_uniform(max - min + 1))
    
#define RGB(r, g, b) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:1.0]
    
#define RGBA(r, g, b, a) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:a]
    
    
    int Integer = RAND_FROM_TO(0, 7);
   
    
    if (segments.selectedSegmentIndex==0) {
        alpha=1.0f;
        
    }
    else if (segments.selectedSegmentIndex==1)
    {
    
        
    }
    
    
    if (Integer==0)
        
    {
        tempcolor=RGBA(255.0f, 255.0f, 0.0f, alpha);
    }
    else if (Integer==1)
    {
        tempcolor=RGBA(255.0f, 0.0f, 0.0f,alpha);
    }
    
    else if (Integer==2)
    {
        tempcolor=RGBA(0.0f, 255.0f, 0.0f,alpha);
        
    }
    
    else if (Integer==3)
    {
        tempcolor=RGBA(0.0f, 255.0f, 255.0f,alpha);
        
    }
    else if (Integer==4)
    {
        tempcolor=RGBA(0.0f, 0.0f, 255.0f,alpha);
        
    }
    else if (Integer==5)
    {
        tempcolor=RGBA(0.0f, 128.0f, 0.0f,alpha);
        
    }
    
    else if (Integer==6)
    {
        tempcolor=RGBA(0.0f, 128.0f, 128.0f,alpha);
        
    }
    else if (Integer==7)
    {
        tempcolor=RGBA(255.0f, 0.0f, 255.0f,alpha);
        
    }
    
    
    [Singleton sharedMySingleton].colorNumber=tempcolor;
    
  
    
    
    UITouch *touch = [touches anyObject];
    
    
    if (touch.tapCount == 1) {
        
        lastPoint = [touch locationInView:self.drawImage];
        
       
        
        
    } else
    {
        
       
            
            drawImage.image = nil;
            
            
            
            
        
    }
    
    
    
    image = drawImage.image;
}





- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    UITouch *touch = [touches anyObject];
    
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    
    UIGraphicsBeginImageContextWithOptions(self.drawImage.frame.size, NO, [[UIScreen mainScreen] scale]);
    
    CGRect drawRect = CGRectMake(0.0f, 0.0f,
                                 self.drawImage.frame.size.width,
                                 self.drawImage.frame.size.height);
    
    [image drawInRect:drawRect];
    
    
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0f);
    
    
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    
  
  
    
    
    if (segments.selectedSegmentIndex==0) {
        
     CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [tempcolor CGColor]);
    
    
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        
        firstPoint=lastPoint;
        endPoint=currentPoint;
        
    }
    
    
    
    if (segments.selectedSegmentIndex==1)
    
    {
        
        
        CGFloat alphas=( arc4random() % 256 / 256.0 );
        
        if (alphas<0.3f) {
            alpha=alphas+0.3f;
        }
        else if (alphas>0.7f) {
            alpha=alphas-0.3f;
        }

        
    
        
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [tempcolor CGColor]);
        
        
        if (touches.count == 2) {
            
    
                
            UITouch *touch1 = [touches allObjects][0];
            UITouch *touch2 = [touches allObjects][1];
            
                CGPoint currentPoint1 = [touch1 locationInView:drawImage.superview];
                
                CGPoint currentPoint2 = [touch2 locationInView:drawImage.superview];
                
            
            
            firstPoint=currentPoint1;;
            endPoint=currentPoint2;
                
            
                
                
                CGPoint one=currentPoint1;
                CGPoint two=currentPoint2;
                
                
                
                CGFloat h=two.y-one.y;
                CGFloat w=two.x-one.x;
                
                CGRect rectangle = CGRectMake(one.x, one.y, w, h);
                
                
                
                
                
                
                
                
                CGContextAddEllipseInRect(UIGraphicsGetCurrentContext(), rectangle);
                
                
                
                
                
            
        }
        else {
            return;
        }
        
    
    
    }
    
    
   
    
   CGContextStrokePath(UIGraphicsGetCurrentContext());
    
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    if (firstPoint.x!=endPoint.x&&firstPoint.y!=endPoint.y) {
        
    
    
    
        float temp=pow(firstPoint.x-endPoint.x,2)-pow(firstPoint.y-endPoint.y, 2);
        
        double length=pow(fabsf(temp),0.5);
        
        
        
        [Singleton sharedMySingleton].length=[[NSNumber numberWithFloat:length] stringValue];
        
        [Singleton sharedMySingleton].firstX=[[NSNumber numberWithFloat:(int)firstPoint.x] stringValue];
        
        [Singleton sharedMySingleton].firstY=[[NSNumber numberWithFloat:(int)firstPoint.y] stringValue];
        
        [Singleton sharedMySingleton].lastX=[[NSNumber numberWithFloat:(int)endPoint.x] stringValue];
        
        [Singleton sharedMySingleton].lastY=[[NSNumber numberWithFloat:(int)endPoint.y] stringValue];
        
        
    if (segments.selectedSegmentIndex==0) {
      
        [Singleton sharedMySingleton].elementNаme=@"Line";
        
    }
    
    if (segments.selectedSegmentIndex==1) {
        
    [Singleton sharedMySingleton].elementNаme=@"Ellipse";
        
    }
    
    
        NSArray *data=[NSArray arrayWithObjects:[Singleton sharedMySingleton].firstX,[Singleton sharedMySingleton].firstY,[Singleton sharedMySingleton].lastX,[Singleton sharedMySingleton].lastY,[Singleton sharedMySingleton].length,[Singleton sharedMySingleton].colorNumber,[Singleton sharedMySingleton].elementNаme, nil];
    
        
        
    
        NSUInteger number=[Singleton sharedMySingleton].arrayMain.count;
        
       
           
            
            
            [[Singleton sharedMySingleton].arrayMain insertObject:data atIndex:number];
        
        
       
        
        
        if ([Singleton sharedMySingleton].arrayMain.count>=2)
            {
                
          
                
                
                if ([[data objectAtIndex:2]floatValue]==0.0f&&[[data objectAtIndex:3]floatValue]==0.0f)
            
            
            {
                
                [[Singleton sharedMySingleton].arrayMain removeObjectAtIndex:number];
                
            }
                
                
         
                
                
        
            
        }

        firstPoint.x=0.0f;
        firstPoint.y=0.0f;
        endPoint.x=0.0f;
        endPoint.y=0.0f;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTable"
                                                            object:self];
        
    
    
    
    
 }
}



@end
  • Вопрос задан
  • 2571 просмотр
Пригласить эксперта
Ответы на вопрос 2
morozovdenis
@morozovdenis
NSArray *data=[NSArray arrayWithObjects:[Singleton sharedMySingleton].firstX,[Singleton sharedMySingleton].firstY,[Singleton sharedMySingleton].lastX,[Singleton sharedMySingleton].lastY,[Singleton sharedMySingleton].length,[Singleton sharedMySingleton].colorNumber,[Singleton sharedMySingleton].elementNаme, nil];

266288__odnazhdy_v_meksike_hdtv_.1_26_16
Ответ написан
@mr_cloud
iOS Developer
Начать учить Objective-C https://vk.com/iosdevcourse
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы