简体   繁体   English

如何在MySQL数据库中存储对象的颜色数据?

[英]How can I store an object's color data in MySQL database?

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. 我有一个iOS Swift应用程序的情况,用户可以通过添加其他视图和/或更改视图的背景颜色来“自定义屏幕”。 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. 当他们点击保存时,我想将每个对象的属性以及父视图的背景颜色存储到MySQL数据库中,以便以后检索和重建视图。 All data updates are done through PHP REST services. 所有数据更新都通过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". 如果我打印()颜色,我会得到类似“UIDeviceRGBColorSpace 0.866667 0.92549 1 1”的内容。 I can also convert it to NSData with the following: 我还可以使用以下代码将其转换为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? 我应该做一些事情,比如抓住RGB值和Alpha并将它们保存到数据库中的4个属性,或者获取十六进制值并存储它? Perhaps, there is yet a different approach that would be even more straight forward? 也许,还有一种不同的方法会更直接吗?

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组件并将它们存储为字符串可能是更好的选择。

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

相关问题 如何将敏感数据安全地存储在MySQL数据库中? - How can I store sensitive data securely in a MySQL database? 如何将数组中的数据存储到mySQL数据库中? - How can I store data from an array into the mySQL database? 如何将mySQL数据库中的数据存储到数组中? - How can I store data from mySQL database into an array? 如何将HTML颜色代码存储在MySQL数据库中,然后搜索范围或颜色? - How can I store HTML color codes in a MySQL database then search for a range or colors? 如何将此日志数据存储在数据库中 - How can I store this log data in database Symfony-如何使用JQuery / AJAX将简单数据存储到MySQL数据库中? - Symfony- How can i store a simple data into MySQL database using JQuery/AJAX?? 如何使用PHP从另一个网站获取数据并将其存储在MySQL数据库中? - How can I use PHP to fetch data from another website and store it in a MySQL database? 如何将MySQL数据库中的数据存储到ID索引的数组中? - How can I store data from MySQL database into array indexed by id? 如何将区域语言 php 数组数据(古吉拉特语、印地语、旁遮普语)存储和检索到 mysql 数据库? - How can I store and retrieve regional language php array data (gujarati, hindi, punjabi) to mysql database? 如何将用户喜欢的页面数据存储到Mysql数据库中 - How should I store the data of the liked pages by a user into Mysql database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM