在iphonesdk中从Plist加载绘制线

在iphonesdk中从Plist加载绘制线,iphone,ios,Iphone,Ios,我正在保存绘制线x和y位置,以便从触摸移动事件中Plist,代码如下: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = YES; NSLog(@"You are drawing"); UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView

我正在保存绘制线x和y位置,以便从触摸移动事件中Plist,代码如下:

- (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;
    }

}
    - (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];
        }


}
此代码将x和y点保存到plist,就像第一个点保存到键0和值x,y点保存到0-25,54和1-56,98…等等

保存代码工作正常。。。!,我从plist加载x和y点的代码如下:

- (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;
    }

}
    - (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];
        }


}
-(iAction)btn\u加载:(id)发送方{
NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);//1
NSString*documentsDirectory=[paths objectAtIndex:0];//2
路径=[DocumentsDirectoryStringByAppendingPathComponent:@“Lines.plist”];
drawImage.image=nil;
NSMutableDictionary*数据=[[NSMutableDictionary alloc]initWithContentsOfFile:path];
//在这里向数据文件添加元素并将数据写入文件
int值;
value=[[data objectForKey:@“Array_count”]intValue];
CGX;
成形术;
对于(int i=1;i您没有保存“换行符”。属性列表可以包含数组。也许您应该收集数组中的每个“图形”。此外,您没有保存颜色。请尝试此

在touchesBegin中,您正在开始一个新的“绘图”,因此创建一个数组来保存该“绘图”的所有点。在touchMove中,添加到该数组中。在touchEnd中,您完成了该操作,因此您可以将该绘图放入文件中,然后在下一个“begin touch”中重新开始

关键是,您需要知道何时开始新的“绘图段”,以便可以“移动”点并再次开始绘图。

您没有保存“换行符”。属性列表可以包含数组。也许您应该将每个“绘图”收集到一个数组中。此外,您也没有保存颜色。请尝试此

在touchesBegin中,您正在开始一个新的“绘图”,因此创建一个数组来保存该“绘图”的所有点。在touchMove中,添加到该数组中。在touchEnd中,您完成了该操作,因此您可以将该绘图放入文件中,然后在下一个“begin touch”中重新开始

关键是您需要知道何时开始新的“绘图段”,以便可以“移动”该点并再次开始绘图