如何在iPhone SDK中保存和加载绘图线条

5

我希望可以永久保存绘图线并从项目资源中加载已保存的绘图线...!

现在,当触摸移动事件发生时,我会获取x和y位置。我想将x和y位置保存在项目的本地资源中,但我对iOS没有经验,不知道该怎么做...!

而且从项目本地资源保存文件中加载已保存的x和y位置...!

我想要像下面这样保存绘图线:

enter image description here

请任何人帮帮我,非常感激...!

谢谢...!

2个回答

3
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if(!mouseSwiped) {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
        UIGraphicsBeginImageContext(ivBack.frame.size);
        [drawImage.image drawInRect:CGRectMake(0,0, self.ivBack.frame.size.width,self.ivBack.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), line);
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [pickColor CGColor]);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());

 NSData *dataImage=UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
        [dataImage writeToFile:[self SavedImage:(@"1.png")] atomically:YES];
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

那肯定能解决你的问题。

2

如果您想存储x和y位置,可以将它们存储到数组中,然后使用writeToFile将其保存到磁盘。这将以plist格式将数据写入磁盘。

NSMutableArray *points = ...
[points writeToFile:someFileInTheBundle atomically:YES];

您还可以将绘图存储为图像并将其写入磁盘。我编写的这个类别应该会对您的绘图代码提供帮助,它为您提供一个图像: https://github.com/kgn/BBlock/blob/master/UIImage%2BBBlock.m


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接