Ios 在Swift 3中将图像写入SQLite
那么直截了当地说问题。将图像写入数据库,然后读取它会产生两组不同的代码。据我所知,代码将图像转换为NSData,然后将其写入DB。然后它从数据库中读回它,然后将它转换回图片形式 下面是“选择”按钮的代码,它将NSData传递给全局变量,以便以后使用Ios 在Swift 3中将图像写入SQLite,ios,sqlite,swift3,Ios,Sqlite,Swift3,那么直截了当地说问题。将图像写入数据库,然后读取它会产生两组不同的代码。据我所知,代码将图像转换为NSData,然后将其写入DB。然后它从数据库中读回它,然后将它转换回图片形式 下面是“选择”按钮的代码,它将NSData传递给全局变量,以便以后使用 @IBAction func btnCCTV1(_ sender: Any) { // Put Your Image URL let url:NSURL = NSURL(string : "http://cctv-sg.com/i
@IBAction func btnCCTV1(_ sender: Any) {
// Put Your Image URL
let url:NSURL = NSURL(string : "http://cctv-sg.com/images/sr/01.jpg")!
// It Will turn Into Data
let imageData : NSData = NSData.init(contentsOf: url as URL)!
// Data Will Encode into Base64
let str64 = imageData.base64EncodedData(options: .lineLength64Characters)
// Now Base64 will Decode Here
let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)!
// turn Decoded String into Data
let dataImage = UIImage(data: data as Data)
// pass the data image to image View.:)
viewImage.image = dataImage
print("====64====")
print (str64)
print(imageData)
GlobalVar.data64 = imageData as NSData
GlobalVar.imageByte=dataImage
}
然后我们就有了将数据保存到数据库的功能
@IBAction func btnSave(_ sender: Any) {
let contactDB = FMDatabase(path: databasePath as String)
print("==image====")
// print(GlobalVar.imageByte)
if (contactDB?.open())! {
let insertSQL = "INSERT INTO CAMLOCATION (camname, latitude, longitude, description, photo) VALUES ('\(txtCamname.text!)', '\(txtLatitude.text!)', '\(txtLongitude.text!)', '\(txtDescription.text!)', '\(GlobalVar.data64)')"
let result = contactDB?.executeUpdate(insertSQL,
withArgumentsIn: nil)
if !result! {
lblResult.text = "Failed to add contact"
print("Error: \(contactDB?.lastErrorMessage())")
} else {
lblResult.text = "Contact Added"
txtCamname.text = ""
txtLatitude.text = ""
txtLongitude.text = ""
txtDescription.text = ""
}
} else {
print("Error: \(contactDB?.lastErrorMessage())")
}
}
最后,使用find函数检索所述结果。它使用相机名称进行搜索
//====================================FIND BUTTON================================
@IBAction func btnFind(_ sender: Any) {
let contactDB = FMDatabase(path: databasePath as String)
print("========btnFind===========")
if (contactDB?.open())! {
let querySQL = "SELECT camname, latitude, longitude, description, photo FROM CAMLOCATION WHERE camname = '\(txtID.text!)'"
//var blob: Data?
print(querySQL)
let results:FMResultSet? = contactDB?.executeQuery(querySQL,
withArgumentsIn: nil)
//http://stackoverflow.com/questions/29067242/display-image-from-sqlite-blob-in-swift
if results?.next() == true {
txtCamname.text = results?.string(forColumn: "camname")
txtLatitude.text = results?.string(forColumn: "latitude")
txtLongitude.text = results?.string(forColumn: "longitude")
txtDescription.text = results?.string(forColumn: "description")
GlobalVar.data64 = results!.data(forColumn: "photo") as NSData
print("$$$$$$$$$$$$$$$$$$$$$$$")
print(GlobalVar.data64)
// let correctPicture = results?.data(forColumn:"photo")
//GlobalVar.imageByteGet = results?(forColumn:"photo")
//blob = results?.data(forColumn: "photo")
// let ima = NSImage.init(data: blob!)
// turn Decoded String into Data
let dataImage = UIImage(data: GlobalVar.data64 as Data)
// pass the data image to image View.:)
viewImage.image = dataImage
// print(imageByteGet)
// viewImage.image = UIImage(data: imageByteGet!)
//GlobalVar.display64 = results!.data(forColumn: "photo")
lblResult.text = "Record Found"
} else {
lblResult.text = "Record not found"
txtCamname.text = ""
txtLatitude.text = ""
txtLongitude.text = ""
txtDescription.text = ""
}
contactDB?.close()
} else {
print("Error: \(contactDB?.lastErrorMessage())")
}
print("========Attempting decoding...===========")
print("========Decoding....Done. Image should appear.===========")
}
问题是,检索结果与我输入的结果不同。感谢您的帮助。谢谢。如果您需要帮助,我建议您只需重新编写代码,将图像存储在磁盘上;对我来说,这是更好的解决办法。如果您这样做,您的DB可以存储到映像的路径 酷炫易学的lib帮助您实现轻松存储。一般算法为:
- 为所有图像选择唯一字段
- 使用SDWebImage的storeForKey将图像保存在磁盘上的某个位置
- 需要时使用imageForKey检索图像