1

我一直在开发一个使用 Pencil Kit 的应用程序,我正在尝试将画布上的绘图存储到 sqlite3 数据库中。为此,我必须将绘图(类型:数据)转换为 UnsafeRawPointer。但是,转换后,当我尝试通过指针访问(打印)图形时,它返回 0 字节而不是 42 字节。我在下面的代码中添加了一些打印语句以及它们返回的内容,希望对您有所帮助。

 // Function that converts drawing data to UnsafeRawPointer
 func dataToPtr(drawing: Data) -> UnsafeRawPointer {
     
     let nsData = drawing as NSData
     print(nsData) // shows 42 bytes

     let rawPtr = nsData.bytes
     print(rawPtr.load(as: Data.self))// Shows 0 bytes
     return rawPtr
 }

// Drawing before conversion
print(canvas.drawing) // Prints: 42 bytes

let drawingPtr = dataToPtr(drawing: canvas.drawing)

// Drawing when accessing the pointer
print(drawingPtr.load(as: Data.self)) // shows 0 bytes

我是制作 iOS 应用程序的初学者,很难快速理解指针。先感谢您。

编辑:保存绘图方法:

func save(canvas: Canvas) {
    connect()
    
    // prepare
    var statement: OpaquePointer!
    
    // update the drawing given the row id
    if sqlite3_prepare_v2(database, "UPDATE drawings SET drawing = ? WHERE rowid = ?", -1, &statement, nil) != SQLITE_OK {
        print("Could not create (update) query")
    }
    
    // bind place holders
    print("DRAWING SAVED: \(canvas.drawing)") // shows 42 bytes
    let drawingPtr = dataToPtr(drawing: canvas.drawing)

    sqlite3_bind_blob(statement, 1, drawingPtr, -1, nil)
    sqlite3_bind_int(statement, 2, Int32(canvas.id))
    
    // execute
    if sqlite3_step(statement) != SQLITE_DONE {
        print("Could not execute update statement")
    }
    
    // finalise
    sqlite3_finalize(statement)
}

我想使用 .load() 将指针转换为数据的方法:

// Function to check if canvas for a certain date is already in the database, if exists, return canvas
func check(selectedDate: Date) -> [Canvas] {
    connect()
    
    var result: [Canvas] = []

    // prepare
    var statement: OpaquePointer!

    if sqlite3_prepare_v2(database, "SELECT rowid, date, drawing FROM drawings WHERE date = ?", -1, &statement, nil) != SQLITE_OK {
        print("Could not create (select) query")
        return []
    }
    
    // bind
    sqlite3_bind_text(statement, 1, NSString(string:dateToStringFormat(dateDate: selectedDate)).utf8String, -1, nil)
    
    // executes
    while sqlite3_step(statement) == SQLITE_ROW {
        
        // change string date into Date date
        let Date_date = stringToDateFormat(stringDate: String(cString: sqlite3_column_text(statement, 1)))
        // if canvas is not empty
        if sqlite3_column_blob(statement, 2) != nil {
            let drawingPtr = sqlite3_column_blob(statement, 2)
            
            result.append(Canvas(id: Int(sqlite3_column_int(statement, 0)), date: Date_date, drawing: drawingPtr!.load(as: Data.self)))
            print("DRAWING NOT NIL")
        }
        else {
            let drawing = Data.init()
            result.append(Canvas(id: Int(sqlite3_column_int(statement, 0)), date: Date_date, drawing: drawing))
            print("DRAWING IS NIL")
        }
    }
    // finalise
    sqlite3_finalize(statement)
    
    return result
}
4

1 回答 1

2

你需要在这里做的是将你的函数体包装在一个withUnsafeBytes

func save(canvas: Canvas) {
    connect()

    let drawingData = canvas.drawing.dataRepresentation()

    drawingData.withUnsafeBytes { drawingBuffer in

        let drawingPtr = drawingBuffer.baseAddress!

        // ... In here you can use drawingPtr, for example:

        sqlite3_bind_blob(statement, 1, drawingPtr, Int32(drawingBuffer.count), nil)

        // ...
    }
}

withUnsafeBytes块内部,您不得引用drawingData自身。在块之外,您不能参考drawingPtr.

关键withUnsafeBytes是它确保了字节的连续表示(如果需要,进行复制),然后为您提供指向在块期间有效的那些字节的指针。该指针在块外无效。您不得将其退回或让它逃逸。但在块内,您可以将其用作void *. 这意味着您必须确保 sqlite3 不会存储drawingPtr超过此块的末尾,这就是为什么您必须将withUnsafeBytes整个准备/完成序列放在周围,而不仅仅是bind_blob语句。

通常,您不能将 UnsafeRawPointers 传递给您自己没有分配的东西。当你认为它确实存在时,他们所指向的东西并不能保证会继续存在。在 Data 的情况下,甚至没有保证它代表单个内存块(例如,Data 可以从 dispatch_data 支持)。您访问数据字节的方式是使用withUnsafeBytes.

你的check函数也有一些错误。首先,您的 NSString 转换是不必要的。这一行:

sqlite3_bind_text(statement, 1, NSString(string:dateToStringFormat(dateDate: selectedDate)).utf8String, -1, nil)

可以写成:

sqlite3_bind_text(statement, 1, dateToStringFormat(dateDate: selectedDate), -1, nil)

Swift 会在传递给带有char *.

此代码完全错误,可能是您得到零字节的原因:

let drawingPtr = sqlite3_column_blob(statement, 2)
result.append(Canvas(id: Int(sqlite3_column_int(statement, 0)), date: Date_date, drawing: drawingPtr!.load(as: Data.self)))

指向 Blob 的指针不是数据。你不能就load这样。你需要知道它有多长。这是您需要的代码:

// Get the pointer
let drawingPtr = sqlite3_column_blob(statement, 2)!

// Get the length
let drawingLength = Int(sqlite3_column_bytes(statement, 2))

// Copy the bytes into a new Data
let drawing = Data(bytes: drawingPtr, count: drawingLength)

// Now construct your Canvas.
result.append(Canvas(id: Int(sqlite3_column_int(statement, 0)), date: Date_date, drawing: drawing))
于 2020-10-05T15:59:19.883 回答