我一直在开发一个使用 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
}