这里可能存在重复问题,但我搜索了但无法找到答案。将PNG保存到磁盘,但将文件路径保存到核心数据
我有一个应用程序接受来自用户的签名并将签名保存为.png文件。这里是我使用的代码:
-(void)saveSignature
{
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
signature = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *pngData = UIImagePNGRepresentation(signature);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"signature.png"];
[pngData writeToFile:filePath atomically:YES]; //Write the file
}
此代码很好。它将签名的.png保存在我的应用程序的文档文件夹中。我需要做的是将文件路径保存到我的Core Data模型并通过它访问它。核心数据。
如果任何人都能指引我走向正确的方向,那会很棒。
什么似乎是问题?您只需将filePath分配给托管对象的字符串属性并保存上下文。 – Adam
这就是我所设想的,但我如何访问它?如果我叫它,核心数据不会返回一个字符串? –