Question

I have a drawing pad application and I am trying to make an undo button for by creating a mutable array that will hold the saved paths to the image created when touches began is called. This is the code that I have so far

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

t++;

mainImages = [[NSMutableArray alloc] init];

NSString *imagePath = [NSString stringWithFormat:@"Documents/imagePath%d.png", t];

savePath = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];

[UIImagePNGRepresentation(_mainImage.image) writeToFile:savePath atomically:YES];


[mainImages addObject:savePath];

    NSLog(@"t is: %d", t);

    NSLog(@"imagePath is: %@", imagePath);

    NSLog(@"savePath is: %@", savePath);

    NSLog(@"contents of mainImages is: %@", mainImages);

mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
}

When I run this, it appears that my new path is not added to the Mutable array, but that it replaces the path previously saved. This is what my debug console reads:

2014-05-06 14:12:20.550 drawingSkills[6709:60b] t is: 1
2014-05-06 14:12:20.553 drawingSkills[6709:60b] imagePath is: Documents/imagePath1.png
2014-05-06 14:12:20.555 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath1.png
2014-05-06 14:12:20.557 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath1.png"
)


2014-05-06 14:12:25.482 drawingSkills[6709:60b] t is: 2
2014-05-06 14:12:25.483 drawingSkills[6709:60b] imagePath is: Documents/imagePath2.png
2014-05-06 14:12:25.485 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath2.png
2014-05-06 14:12:25.487 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath2.png"
)


2014-05-06 14:19:42.799 drawingSkills[6709:60b] t is: 3
2014-05-06 14:19:42.800 drawingSkills[6709:60b] imagePath is: Documents/imagePath3.png
2014-05-06 14:19:42.802 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath3.png
2014-05-06 14:19:42.804 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath3.png"
)

Can anyone tell me how i can ADD the path to the mutable array instead of replacing the previous path?

Thank you.

Was it helpful?

Solution

Every time that touchesBegan:withEvent: is invoked, the following line in your code creates a new array, overwriting the previously created array:

mainImages = [[NSMutableArray alloc] init];

The array can therefore never have more than one entry. You must change your code so that the array is allocated only once. I suggest you move the line above to the initializer of your custom view class (typically initWithFrame:).

If you absolutely must allocate the array within touchesBegan:withEvent:, try the following rather crude solution:

static bool mainImagesAlreadyAllocated = false;
if (! mainImagesAlreadyAllocated)
{
  mainImages = [[NSMutableArray alloc] init];
  mainImagesAlreadyAllocated = true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top