ios 在SQLite中保存UIBezierPath

xpcnnkqh  于 11个月前  发布在  iOS
关注(0)|答案(3)|浏览(114)

我正在使用UIBezierPath在视图上绘制手绘注解。我想将其保存到SQLite中,以便在用户打开视图时在该视图上绘制。有没有办法将UIBezierPath保存到SQLite中,这样我就可以随时使用它在视图上绘制。

dwthyt8l

dwthyt8l1#

您可以将其序列化为数据(以 NSData 示例的形式),因为 UIBezierPath 符合 NSCoding(即

NSData * data = [NSData archivedDataWithRootObject:*yourPath*]

字符串
另一个选项可能是转换为SVG或从SVG转换并保存。

xxhby3vn

xxhby3vn2#

您可以保存UIBezierPath的所有控制点,而不是直接保存路径。

e7arh2l6

e7arh2l63#

您可以通过定义路径模型并使用Codable来序列化/并行化UIBezierPath。虽然将lineWidthlineCapStyle这样的属性设置为Codable很简单,但路径元素(存储在UIBezierPath.cgPath中)的代码要复杂得多。然而,这里是一个起点:

enum PathElement: Codable, Equatable {
    case moveToPoint(CGPoint)
    case addLineToPoint(CGPoint)
    case addQuadCurveToPoint(controlPoint: CGPoint, endPoint: CGPoint)
    case addCurveToPoint(controlPoint1: CGPoint, controlPoint2: CGPoint, endPoint: CGPoint)
    case closeSubpath
}

extension CGPath {
    static func pathFromPathElements(_ pathElements: [PathElement]) -> CGPath {
        let path = CGMutablePath()

        for pathElement in pathElements {
            switch pathElement {
            case .moveToPoint(let point):
                path.move(to: point)
            case .addLineToPoint(let point):
                path.addLine(to: point)
            case .addQuadCurveToPoint(let controlPoint, let endPoint):
                path.addQuadCurve(to: endPoint, control: controlPoint)
            case .addCurveToPoint(let controlPoint1, let controlPoint2, let endPoint):
                path.addCurve(to: endPoint, control1: controlPoint1, control2: controlPoint2)
            case .closeSubpath:
                path.closeSubpath()
            }
        }
        return path
    }

    func pathElements() -> [PathElement] {
        var result = [PathElement]()

        applyWithBlock { elementPointer in
            let element = elementPointer.pointee
            switch element.type {
            case .moveToPoint:
                let points = Array(UnsafeBufferPointer(start: element.points, count: 1))
                let el = PathElement.moveToPoint(points[0])
                result.append(el)
            case .addLineToPoint:
                let points = Array(UnsafeBufferPointer(start: element.points, count: 1))
                let el = PathElement.addLineToPoint(points[0])
                result.append(el)
            case .addQuadCurveToPoint:
                let points = Array(UnsafeBufferPointer(start: element.points, count: 2))
                let el = PathElement.addQuadCurveToPoint(
                    controlPoint: points[0], endPoint: points[1])
                result.append(el)
            case .addCurveToPoint:
                let points = Array(UnsafeBufferPointer(start: element.points, count: 3))
                let el = PathElement.addCurveToPoint(
                    controlPoint1: points[0], controlPoint2: points[1], endPoint: points[2])
                result.append(el)
            case .closeSubpath:
                result.append(.closeSubpath)
            @unknown default:
                fatalError()
            }
        }

        return result
    }
}

字符串

相关问题