Ios 将石英/岩芯图形图形保存到coredata或文本文件,并再次加载以保持图形
这是我的第一个问题。我有一个记事本应用程序,你可以在上面画画,改变钢笔大小,改变颜色,等等 在检测到触摸后,在视图的drawRect:method中的核心图形代码中进行绘制。现在,我想能够保存这个图纸的核心数据或文本文件不重要,并能够再次加载它,并保持绘图。我可以用ImagePngResentation将其保存为NSDATA吗?那是位图,我不能再继续画画了 请帮助我做这件事的最好方法是什么,保存图形,然后能够再次加载以继续绘制Ios 将石英/岩芯图形图形保存到coredata或文本文件,并再次加载以保持图形,ios,core-graphics,quartz-graphics,Ios,Core Graphics,Quartz Graphics,这是我的第一个问题。我有一个记事本应用程序,你可以在上面画画,改变钢笔大小,改变颜色,等等 在检测到触摸后,在视图的drawRect:method中的核心图形代码中进行绘制。现在,我想能够保存这个图纸的核心数据或文本文件不重要,并能够再次加载它,并保持绘图。我可以用ImagePngResentation将其保存为NSDATA吗?那是位图,我不能再继续画画了 请帮助我做这件事的最好方法是什么,保存图形,然后能够再次加载以继续绘制 非常感谢 我知道现在已经晚了,但我自己花了几个小时才弄明白。下面是:
非常感谢 我知道现在已经晚了,但我自己花了几个小时才弄明白。下面是: 在nib/xib文件(现在是故事板)中,您可以通过在interface Builder中构建接口来“冻结”文件中的视图。本质上,它在这里所做的是归档这些对象并将它们存储在一个文件中。如果用户可以对应用程序中的视图执行此操作,那不是很好吗?嗯,他们可以 苹果公司的指南真的很有帮助。我从头到尾读了一遍 对于您拥有的每个UIView子类,实现
encodeWithCoder:
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
// Encode any objects you want to load later.
// For example: [aCoder encodeObject:colors forKey:@"colors"];
// Make sure they conform to the NSCoder protocol!
}
请注意:有些对象不符合NSCoder协议。例如,我在一些UIView子类中使用UIGestureRecognitor对象。因为它们不符合协议,所以我只是在视图未使用initWithCoder:
存档时重新创建它们。因此,请确保您有一种检索无法存档的对象的方法
在initWithCoder
中:
- (void)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialize your view
// Restore any objects that you encoded
// For example: self.colors = [aDecoder decodeObjectForKey:@"colors"];
}
return self;
}
要保存视图,请执行以下操作:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
- (IBAction)tapSave:(id)sender {
NSString *directory = [self applicationDocumentsDirectory];
NSString *archivePath = [directory stringByAppendingPathComponent:@"<filename>.archive"];
BOOL result = [NSKeyedArchiver archiveRootObject:<your root view> toFile:archivePath];
}
-(NSString*)应用程序文档目录{
返回[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
}
-(iAction)点击保存:(id)发件人{
NSString*目录=[self-applicationDocumentsDirectory];
NSString*archivePath=[目录stringByAppendingPathComponent:@.archive];
BOOL result=[NSKeyedArchiver archiveRootObject:toFile:archivePath];
}
最后,要加载视图及其子视图,请执行以下操作:
- (IBAction)tapLoad:(id)sender {
NSString *directory = [self applicationDocumentsDirectory];
NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"<filename>.archive"];
unarchivedView = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
}
-(iAction)点击加载:(id)发送方{
NSString*目录=[self-applicationDocumentsDirectory];
NSString*archivePath=[NSTemporaryDirectory()stringByAppendingPathComponent:@.archive];
unarchivedView=[NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
}
希望这有帮助 我不希望它是位图,因为我想能够缩放它,删除部分,添加更多的图形,等等