[英]how to save image into sqlite3 using swift
I want to store an image into sqlite3. 我想将图像存储到sqlite3中。 I am new to iOS please help me out how to save this image and retrieve in image view.
我是iOS的新手,请帮助我如何保存该图像并在图像视图中检索。
Appdelegate.swift Appdelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
createDB()
return true
}
func createDB()
{
// create db
let dir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let dbpath = dir.appendingPathComponent("imgdatabase.sqlite")
print(dir)
// check if file exist
let m = FileManager()
if m.fileExists(atPath: dbpath.path)
{
print("file exist no need to create")
}
else
{
m.createFile(atPath: dbpath.path, contents: nil, attributes: nil)
}
// open db
var op : OpaquePointer? = nil
if sqlite3_open(dbpath.path, &op)==SQLITE_OK
{
print("db open successfuly")
let query = "create table img_save(img text)"
if sqlite3_exec(op, query.cString(using: .utf8), nil, nil, nil)==SQLITE_OK
{
print("table created ")
}
else
{
print("table not created")
}
}
else
{
print("db unable to open")
}
sqlite3_close(op)
}
Image_save_ViewController.swift Image_save_ViewController.swift
import UIKit
class Image_save_ViewController: UIViewController {
@IBAction func img_display_click(_ sender: AnyObject) {
}
@IBOutlet weak var imgview: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func img_save_click(_ sender: AnyObject) {
let image : UIImage = UIImage(named: "35-handgun-png-image.png")!
let imageData : NSData = UIImagePNGRepresentation(image)! as NSData
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
var decodeimg : NSData = NSData(base64Encoded: strBase64, options: NSData.Base64DecodingOptions(rawValue: 0))!
imgview.image = UIImage(data : decodeimg as Data)!
let dir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let dbpath = dir.appendingPathComponent("imgdatabase.sqlite")
print(dir)
// check if file exist
let m = FileManager()
if m.fileExists(atPath: dbpath.path)
{
print("file exist no need to create")
}
else
{
m.createFile(atPath: dbpath.path, contents: nil, attributes: nil)
print("db file created")
}
// open db
var op : OpaquePointer? = nil
if sqlite3_open(dbpath.path, &op)==SQLITE_OK
{
print("db open successfuly")
let image_d = strBase64
let query = String.init(format : "insert into img_save values('%@')", image_d)
if sqlite3_exec(op, query.cString(using: .utf8), nil, nil, nil)==SQLITE_OK
{
print("image saved successfuly")
}
else
{
print("unable to save")
}
}
else
{
print("unable to open db")
}
sqlite3_close(op)
}
I AM ADDING HERE READDB() FUNC THAT I AM FETCHING IMAGE FROM DB 我在这里添加了READDB()函数,即正在从DB读取图像
@IBOutlet weak var imgview: UIImageView! @IBOutlet弱var imgview:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
readDB()
//imgview.image = UIImage(named: "35-handgun-png-image.png")
}
func readDB()
{
let image : UIImage = UIImage(named: "35-handgun-png-image.png")!
let imageData = UIImagePNGRepresentation(image)!
let docdir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
print(docdir)
let imgurl = docdir.appendingPathComponent("35-handgun-png-image.png")
try! imageData.write(to: imgurl)
let newImage = UIImage(contentsOfFile: imgurl.path)
let dir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let dbpath = dir.appendingPathComponent("imgdatabase.sqlite")
//check file if exist
let m = FileManager()
if m.fileExists(atPath: dbpath.path)
{
print("file exist no need to create")
}
else
{
m.createFile(atPath: dbpath.path, contents: nil, attributes: nil)
print("file created")
}
//open db
var opq : OpaquePointer? = nil
if sqlite3_open(dbpath.path, &opq)==SQLITE_OK
{
let query = "select * from img_save"
var st : OpaquePointer? = nil
if sqlite3_prepare(opq, query.cString(using: .utf8), -1, &st, nil)==SQLITE_OK
{
while sqlite3_step(st)==SQLITE_ROW
{
let imgd = String.init(format : "%s",sqlite3_column_text(st, 0))
//let imgd = String.init(format : "%s",sqlite3_value_text(st!))
imgview.image = UIImage(named : imgd)
print("nil value")
}
}
}
sqlite3_close(opq)
}
EARLIER IT WAS SHOWING FATAL ERROR FOUND NIL WHILL UNWRAPPING VALUES BUT NOW I HAVE TAKEN IMAGE VIEW TO STORE COLUM VALUE AND DISPLAY IT USING IMAGE VIEW..SO PLEASE TELL ME HOW TO ACHIEVE THIS I AM GETTING NIL VALUE OUT THERE 较早时它会显示出致命错误,但会立即取消零值,但现在我已经进行了图像视图以存储列值并使用图像视图进行显示。因此,请告诉我如何获得零值
If possible you write your image in application document directory by specific name. 如果可能,您可以按特定名称在应用程序文档目录中写入图像。 And save that name in sqlite table.
并将该名称保存在sqlite表中。 Retrieve name from sqlite and image from document directory.
从sqlite检索名称,从文档目录检索图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.