0

に描画するためのこのコードがあります。描画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];
    }
}

ありがとう。

4

1 に答える 1

2
       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];
    }
于 2012-11-21T11:37:55.657 に答える