M
M
Max Onflic2014-09-21 14:12:01
Objective-C
Max Onflic, 2014-09-21 14:12:01

How to avoid memory leak?

Actually, in the ImageView ViewController, the user can draw lines and ellipses. In the touchesMoved method, a bunch of images are constantly created, which clog the RAM. How to avoid it?

@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а[email protected]"Line";
        
    }
    
    if (segments.selectedSegmentIndex==1) {
        
    [Singleton sharedMySingleton].elementNа[email protected]"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

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Denis Morozov, 2014-09-21
@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].elementName, nil];

266288__odnazhdy_v_meksike_hdtv_.1_26_16

V
Vanya Ivanov, 2015-01-04
@mr_cloud

Start learning Objective-C https://vk.com/iosdevcourse

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question