从iPhone SDK中的Plist绘制线条加载 [英] Draw Lines Load From Plist in iphone sdk

查看:57
本文介绍了从iPhone SDK中的Plist绘制线条加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 touchesMoved事件中的绘制线x和y位置保存到Plist中:

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

}

此代码将x和y点保存到plist中,如将第一个点保存到键0并将值x,y点保存为0-25,54和1-56,98 ..... etc.

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.

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

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:

我从plist输出中加载的x和y点如下:

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

我的问题是从plist加载画线不完全像最后的x和y点问题..!

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

我想要输出从x和y点绘制的完美线条..!

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

请非常感谢我们与我们一起帮助..!

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

谢谢..

推荐答案

您没有保存换行符".属性列表可以包含数组.也许您应该将每个图形"收集到一个数组中.此外,您没有保存颜色.试试这个...

You are not saving your "line breaks." A property list can contain arrays. Maybe you should collect each "drawing" in an array itself. Also, you are not saving the color. Try this...

在touchesBegin中,您将开始一个新的图形",因此创建一个数组来保存该图形"的所有点.在touchMove中,添加到该数组.在touchEnd中,您已经完成了操作,因此可以将该图形放置到文件中,然后从下一个开始触摸"开始.

In touchesBegin, you are starting a new "drawing" so create an array to hold all points for that "drawing." In touchMove, add to that array. in touchEnd, you are done with that, so you can put that drawing to the file, and start over on the next "begin touch."

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

The key is that you need to know when you start a new "drawing segment" so you can "move" the point and start drawing again.

这篇关于从iPhone SDK中的Plist绘制线条加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆