ios - 如何将 CGPaths 持久化到 CoreData

标签 ios swift core-graphics

我正在 iOS 上制作绘图应用程序,并希望将用户绘图保存到核心数据。

我知道 NSData 是 CoreData 的一种属性类型,所以我正在考虑使用它。

我的想法是以某种方式将我的 CGPath 数组转换为 NSData,然后将 NSData 保存到 Core Data。有没有人知道我将如何执行此操作或有更好的解决方案?

最佳答案

如果您将 CGPath 转换为支持 NSCoding/NSSecureCodingUIBezierPath:

do {
    let path = UIBezierPath(cgPath: cgPath)
    let data = try NSKeyedArchiver.archivedData(withRootObject: path, requiringSecureCoding: false)
} catch {
    print(error)
}

您还可以将 Data 转换回 UIBezierPath:

do {
    if let path = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIBezierPath.self, from: data) {
        let cgPath = path.cgPath
    }
} catch {
    print(error)
}

关于ios - 如何将 CGPaths 持久化到 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53992867/

相关文章:

ios - "How to use Codable Protocol for a Networking Layer "

ios - 将 UITableView 和 CollectionView 合并到一个 View 中

ios - Swift 组合 : How to handle event and continue stream

swift - UIBezierPath 的快速替代品

ios - 如何提高CGLayer的绘图质量

ios - 在 pdf 上添加注释

For循环中的iOS 7 CA层旋转

ios - 资源文件未随 Xcode 5 更新

ios - iOS 7.0 中的水平 CAGradientLayer

ios - 我可以发布一个未完成的应用程序吗?