I have a situation in an iOS Swift app where the user can "customize the screen" by adding other views and/or changing the view's background color. When they tap save, I want to store the attributes of each object, as well as the parent view's background color to a MySQL database for later retrieval and reconstruction of the views. All data updates are done through PHP REST services.
我有一个iOS Swift应用程序的情况,用户可以通过添加其他视图和/或更改视图的背景颜色来“自定义屏幕”。当他们点击保存时,我想将每个对象的属性以及父视图的背景颜色存储到MySQL数据库中,以便以后检索和重建视图。所有数据更新都通过PHP REST服务完成。
I'm currently struggling with the color data for the background color. If I print() the color, I get something like "UIDeviceRGBColorSpace 0.866667 0.92549 1 1". I can also convert it to NSData with the following:
我目前正在努力寻找背景颜色的颜色数据。如果我打印()颜色,我会得到类似“UIDeviceRGBColorSpace 0.866667 0.92549 1 1”的内容。我还可以使用以下代码将其转换为NSData:
let data = NSKeyedArchiver.archivedDataWithRootObject(self.view.backgroundColor!)
However, in either case, I have no idea how to save the data to the database via PHP REST service or even what datatype I would use.
但是,在任何一种情况下,我都不知道如何通过PHP REST服务将数据保存到数据库,甚至不知道我将使用哪种数据类型。
Am I going down the wrong path, altogether? Should I be doing something like grab the RGB values and Alpha and save those to 4 attributes in the database, or get the hex value and store that? Perhaps, there is yet a different approach that would be even more straight forward?
我完全走错了路吗?我应该做一些事情,比如抓取RGB值和Alpha并将它们保存到数据库中的4个属性,或者获取十六进制值并存储它?也许,还有一种不同的方法会更直接吗?
1 个解决方案
#1
0
If you care about retaining all the information encoded in that NSColor / UIColor instance (original precise value in its original colour space which may not be RGB, calibration etc) and need to encode it as a string (instead of for instance binary blob, which is possible with MySQL too), you could use base64 encoding – send your server the colour in base64 encoded form, and decode it back when retrieving.
如果您关心保留NSColor / UIColor实例中编码的所有信息(原始颜色空间中的原始精确值,可能不是RGB,校准等),需要将其编码为字符串(而不是例如二进制blob,其中也可以使用MySQL),您可以使用base64编码 - 以base64编码形式向服务器发送颜色,并在检索时将其解码回来。
let color = NSColor.blackColor()
let data = NSKeyedArchiver.archivedDataWithRootObject(color)
// this can go in your database
let base64EncodedColorString = data.base64EncodedStringWithOptions([])
let decodedColorData = NSData(base64EncodedString: base64EncodedColorString, options: [])
NSKeyedUnarchiver.unarchiveObjectWithData(decodedColorData!)
This has the obvious downside that your server will have no clue about what that data encodes, so if you care about that, then getting the colour's RGB components and storing those as a string is probably the better option.
这有明显的缺点,你的服务器将不知道该数据编码的内容,所以如果你关心它,那么获取颜色的RGB组件并将它们存储为字符串可能是更好的选择。
#1
0
If you care about retaining all the information encoded in that NSColor / UIColor instance (original precise value in its original colour space which may not be RGB, calibration etc) and need to encode it as a string (instead of for instance binary blob, which is possible with MySQL too), you could use base64 encoding – send your server the colour in base64 encoded form, and decode it back when retrieving.
如果您关心保留NSColor / UIColor实例中编码的所有信息(原始颜色空间中的原始精确值,可能不是RGB,校准等),需要将其编码为字符串(而不是例如二进制blob,其中也可以使用MySQL),您可以使用base64编码 - 以base64编码形式向服务器发送颜色,并在检索时将其解码回来。
let color = NSColor.blackColor()
let data = NSKeyedArchiver.archivedDataWithRootObject(color)
// this can go in your database
let base64EncodedColorString = data.base64EncodedStringWithOptions([])
let decodedColorData = NSData(base64EncodedString: base64EncodedColorString, options: [])
NSKeyedUnarchiver.unarchiveObjectWithData(decodedColorData!)
This has the obvious downside that your server will have no clue about what that data encodes, so if you care about that, then getting the colour's RGB components and storing those as a string is probably the better option.
这有明显的缺点,你的服务器将不知道该数据编码的内容,所以如果你关心它,那么获取颜色的RGB组件并将它们存储为字符串可能是更好的选择。