1
これで問題はまったく解決しました。イメージをDBに書き込んだ後、それを読み取ると、2つの異なるコードセットが生成されます。コードは画像をNSDataに変換し、それを私が理解しているものからDBに書き出します。その後、それをDBから読み込んで、それを再び画像形式に変換します。Swift 3でSQLiteに画像を書き込む
「選択」ボタンのコードは、NSDataを後で使用するグローバル変数に渡します。
@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.===========")
}
「検索」とは、入力した内容とは異なる結果をもたらします。どんな助けもありがとうございます。ありがとう。
うーんを必要なときにimageForKeyを使用して画像を取得するディスク