简体   繁体   English

快速将NSImage与FMDB保存在一起

[英]save NSImage with FMDB in swift

I'm working on a CarRental App for OS X in swift. 我正在快速开发适用于OS X的CarRental应用程序。 I have a NSImageView where the user can drop a picture and some TextFields. 我有一个NSImageView,用户可以在其中放置图片和一些TextField。 The car data are stored in an array of Car Objects. 汽车数据存储在汽车对象数组中。 I need to write this data to an SQLite database with FMDB. 我需要将此数据写入带有FMDB的SQLite数据库。 I have no problems with the text, but how to save the picture? 我的文字没有问题,但是如何保存图片? The below code runs without error but it does not save the image correctely. 下面的代码运行无误,但不能正确保存图像。

let sql = "INSERT INTO tblCars (cMakeModel, cPrice, cPhoto) VALUES (?,?,?)"

    if let db = DBManager.openDB(dbName) {

        for var i = 0; i < carsArray.arrangedObjects.count; ++i {
            let car = carsArray.arrangedObjects[i] as CarData

            let ok = db.executeUpdate(sql, withArgumentsInArray: [car.makeModel, car.price, car.photo!])

            if !ok {
                println("Error: \(db.lastErrorMessage())")
                return
            }
        }
        println("Car added")
    }

How to save an Image with FMDB? 如何使用FMDB保存图像?

Do not save the entire image in the FMDB. 不要将整个图像保存在FMDB中。

Save the image in the sandbox and simply save the image path in FMDB. 将图像保存在沙箱中,然后只需将图像路径保存在FMDB中。

More info on saving the image in sandbox here 有关在此处将图像保存在沙箱中的更多信息

How do I save an image to the sandbox - iPhone SDK 如何将图像保存到沙箱-iPhone SDK

FMDB can save NSData objects in SQLite as BLOBs. FMDB可以节省NSData在SQLite的对象为BLOB。 So, the only question is how to get NSData representation (eg PNG, JPEG, TIFF, etc.) of the image. 因此,唯一的问题是如何获取图像的NSData表示形式(例如PNG,JPEG,TIFF等)。

If possible, one should use the original digital asset you used to create the NSImage in the first place. 如果可能的话,应该首先使用用于创建NSImage的原始数字资产。 For example, if you loaded it from a PNG or JPEG file, go back and get the NSData from that file. 例如,如果您是从PNG或JPEG文件加载的,请返回并从该文件获取NSData

If you only have a NSImage object, you can create a representation for that image. 如果只有NSImage对象,则可以为该图像创建一个表示。 This generally not ideal because you often can make the asset bigger than it was originally and/or you can introduce quality loss. 这通常是不理想的,因为您通常可以使资产比原来大,并且/或者您可能引入质量损失。

If you wanted to get a PNG representation of the image, though, you could do something like: 但是,如果您想获取图像的PNG表示形式,则可以执行以下操作:

func PNGRepresentation(image: NSImage) -> NSData? {
    if let TIFFRepresentation = image.TIFFRepresentation, bitmap = NSBitmapImageRep(data: TIFFRepresentation) {
        return bitmap.representationUsingType(.NSPNGFileType, properties: [:])
    }

    return nil
}

As Rajeev points out, if your images are large (ie not thumbnail sized) SQLite is poorly suited for storing large binary objects. 正如Rajeev指出的那样,如果您的图像很大(即不是缩略图大小),则SQLite不适合存储大型二进制对象。 So you might store the digital asset in the file system (eg in your app's sandbox) and then only store a relative reference to that file in the SQLite database. 因此,您可以将数字资产存储在文件系统中(例如,在应用程序的沙箱中),然后仅在SQLite数据库中存储对该文件的相对引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM