2017-10-16 24 views
-1

我想將圖像存儲到sqlite3中。我是iOS新手,請幫助我解決如何保存圖像並在圖像視圖中檢索。如何使用swift將圖像保存到sqlite3中

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,我取映像從DB

@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) 

} 

以前它是SHOWING致命錯誤FOUND NIL WHILL展開值,但現在我已經拍攝的圖像以期STORE COLUM值,並顯示IT使用圖像VIEW..SO請告訴我如何做到這一點我得到空值OUT THERE

+3

但你在哪裏看到這個錯誤? –

+0

in image_save_ view controller –

+0

where?告訴我們你的問題是什麼,什麼是零 –

回答

0

如果可能的話,您可以通過特定的名稱在應用程序文檔目錄中編寫圖像。 並將該名稱保存在sqlite表中。從sqlite中獲取名字並從文檔目錄中獲取圖像。

+0

@ Sanjeev可以用example來解釋我 –

+0

首先你在文檔目錄中寫這樣的圖像 讓documentsDirectoryURL =試試! NSFileManager()。URLForDirectory(.DocumentDirectory,inDomain:.UserDomainMask,appropriateForURL:nil,create:true)//創建名稱 let fileURL = documentsDirectoryURL.URLByAppendingPathComponent(「Image.jpg」)if!NSFileManager.defaultManager()。fileExistsAtPath (fileURL.path!){if UIImageJPEGRepresentation(image,1.0)!writeToFile(fileURL.path !, atomically:true){print(「saved」)} else {print(「error」)}} else {print(「文件已存在「)} 在sqlite表中保存這個名字」Image.jpg「 –

+0

Thnx @Saneev先生 –

相關問題