1

I am Save the Draw lines x and y position to Plist from touchesMoved event Below Code:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    NSLog(@"You are drawing");
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;


    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(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 1.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSNumber *numx = [NSNumber numberWithFloat:currentPoint.x];
    NSNumber *numy = [NSNumber numberWithFloat:currentPoint.y];
    [xpoints insertObject:numx atIndex:[xpoints count]];
    [ypoint insertObject:numy atIndex:[ypoint count]];

    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Lines" ofType:@"plist"]; //5

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    path = [documentsDirectory stringByAppendingPathComponent:@"Lines.plist"];


    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    //here add elements to data file and write data to file
    int value = [xpoints count];

    [data setObject:[NSNumber numberWithInt:value] forKey:@"Array_count"];
    NSString *obj_str=[NSString stringWithFormat:@"%@,%@",numx,numy];
    NSString *key=[NSString stringWithFormat:@"%d",value];
    [data setObject:obj_str forKey:key];



    [data writeToFile:path atomically:YES];
    [data release];

    NSString *string = [NSString stringWithFormat:@"%f%@%f", lastPoint.x, @"-",lastPoint.y];
   // NSLog(@"Hello is %i",[xpoints count]);
    lblxy.text=string;

    NSString *string2 = [NSString stringWithFormat:@"%f%@%f", currentPoint.x, @"-",currentPoint.y];
   // NSLog(@"Hello is %i",[xpoints count]);
    lbllast.text=string2;


    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }

}

This code Saves x and y points to plist like first point save to key 0 and value x ,y points like 0-25,54 and 1-56,98 .....etc.

Save code is Working Perfectly...!,My load x and y points from plist Below Code:

    - (IBAction)btn_load:(id)sender {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
        NSString *documentsDirectory = [paths objectAtIndex:0]; //2
        path = [documentsDirectory stringByAppendingPathComponent:@"Lines.plist"];

        drawImage.image =nil;


        NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

        //here add elements to data file and write data to file    
        int value;
        value = [[data objectForKey:@"Array_count"] intValue];

        CGFloat lastx;
        CGFloat lasty;

        for(int i=1;i<=value;i++)
        {
            NSString *key=[NSString stringWithFormat:@"%d",i];
            NSLog(@"This is from .plist %@", [data objectForKey:key]);

            NSString *my_points=[data objectForKey:key];
            NSArray *components = [my_points componentsSeparatedByString:@","];
            [[components objectAtIndex:0] floatValue];

            if(i==1)
            {
                lastx = [[components objectAtIndex:0] floatValue];
                lasty = [[components objectAtIndex:1] floatValue];
            }

            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(), 5.0);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastx , lasty);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[components objectAtIndex:0] floatValue], [[components objectAtIndex:1] floatValue]);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            lastx = [[components objectAtIndex:0] floatValue];
            lasty = [[components objectAtIndex:1] floatValue];
        }


}

My output is Drawing Below:

enter image description here

My loading x and y points from plist output is Below:

enter image description here

My Problem is Loading Draw line from plist not perfectly like last x and y points problem..!

I want output Perfect for Drawing lines from x and y points..!

Please help with me any one us with greatly appreciated..!

Thanks..!

4

1 に答える 1

1

「改行」を保存していません。プロパティリストには配列を含めることができます。たぶん、あなたはそれぞれの「図面」を配列自体に集めるべきです。また、あなたは色を保存していません。これを試して...

touchesBeginでは、新しい「描画」を開始しているので、その「描画」のすべてのポイントを保持する配列を作成します。touchMoveで、その配列に追加します。touchEndでは、これで完了です。その図面をファイルに配置して、次の「最初のタッチ」からやり直すことができます。

重要なのは、ポイントを「移動」して再び描画を開始できるように、新しい「描画セグメント」をいつ開始するかを知る必要があるということです。

于 2012-04-14T06:48:12.947 に答える