我有用于在UIImageView
上绘制的代码,我想将图形保存到数组中并添加一个按钮以删除最后一个图形。
我怎样才能做到这一点?
这是我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 10;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 10;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
drawImage.backgroundColor = [UIColor clearColor];
}
}
谢谢。
最佳答案
Declare on NSMutableArray object in .h file
// in .h file
NSMutableArray *storage;
// in .m file and initialize in view did load
storage = [[NSMutableArray alloc]init];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self.storage count] < MAX_LINE) {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
drawImage.backgroundColor = [UIColor clearColor];
}
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%d_drawslide__.png", [self.storage count]]];
[self.storage addObject:path];
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:drawImage.image, path, nil];
[NSThread detachNewThreadSelector:@selector(backuppmage:) toTarget:self withObject:params];
[params release];
}
}
-(void) backuppmage:(NSMutableDictionary *) params
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSString *key in params) {
UIImage *image = (UIImage *)[params objectForKey:key];
[UIImagePNGRepresentation(image) writeToFile:key atomically:YES];
}
[pool drain];
}
// add this method to button click
-(IBAction) undoEraseDrawing:(id) sender {
if (!isErased && [self.storage count] > 0) {
[self.storage removeObject:[self.storage lastObject]];
}
if ([self.storage count] > 0 && [self.storage lastObject] != nil) {
drawImage.image = [self getImageWithURL:(NSString *)[self.storage lastObject]];
} else {
drawImage.image = nil;
}
isErased = NO;
}
-(UIImage *) getImageWithURL:(NSString *)url
{
return [UIImage imageWithContentsOfFile:url];
}