FMDB can save NSData objects in SQLite as a BLOB. So, the only question is how to get the presentation of NSData (like PNG, JPEG, TIFF, etc.) Images.
If possible, you should use the original digital asset that you used to create NSImage in the first place. For example, if you downloaded it from a PNG or JPEG file, go back and get NSData from this file.
If you only have an NSImage object, you can create a view for this image. This is usually not ideal, because you can often make your asset larger than it was originally, and / or you can introduce quality losses.
If you want to get a PNG representation of an image, you can do something like:
func PNGRepresentation(image: NSImage) -> NSData? { if let TIFFRepresentation = image.TIFFRepresentation, bitmap = NSBitmapImageRep(data: TIFFRepresentation) { return bitmap.representationUsingType(.NSPNGFileType, properties: [:]) } return nil }
As Rajiv points out, if your images are large (i.e., the size is not reduced), SQLite is poorly suited for storing blobs. Thus, you can save the digital asset in the file system (for example, in the sandbox of your application), and then save only the relative link to this file in the SQLite database.
source share