我想将图像存储到sqlite3中。我是iOS新手请帮我解决如何保存此图像并在图像视图中检索。
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
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)
}
我在这里添加READDB()FUNC我从数据库中获取图像
@IBOutlet weak 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)
}
早期发现致命错误发现致命错误,但现在我已经采用图像视图来存储柱值并使用图像视图显示它。请告诉我如何实现这一点我将获得无价值
答案 0 :(得分:0)
如果可能,您可以按应用名称在应用程序文档目录中编写图像。 并在sqlite表中保存该名称。从sqlite中检索名称,从文档目录中检索图像。