复制和序列化 Quartz 引用的最佳实践
我在 Cocoa 中有包含 Quartz-2D 引用(描述颜色、填充图案、渐变和阴影)的对象。我想在我的对象中实现 NSCoding 协议,因此需要序列化那些不透明的 Quartz-2D 结构。
可能的解决方案可能是:
在我的对象中定义一组属性,允许在需要时从头开始设置数据结构。然后可以轻松地将它们序列化。 示例:存储红色、绿色、蓝色和 Alpha 的四个浮点,然后使用 CGColorCreate 。 缺点:信息重复,因此存在潜在的一致性和(到目前为止较小的)空间消耗问题。我需要手动编写属性设置器,以便在组件发生更改时重新创建 Quartz 结构。这会使我的代码大大膨胀。
使用 Quartz 函数读取属性。 示例:使用
CGColorGetComponents
获取颜色。 缺点:它似乎适用于颜色。但是其他结构没有等效的函数,所以我不知道这如何适用于渐变、阴影、阴影等。直接从原始的、不透明的结构中读出属性。 缺点:正如文档所述,结构应该是不透明的。因此,如果底层发生变化,我的代码就会崩溃。 (如果应该这样做的话,Apple 肯定不会提供像 CGColorGetComponents 这样的函数。)此外,诸如
CGShadingRef
内的CGFunctionRef
之类的东西真的会带来麻烦。
序列化 Quartz 结构的最佳实践是什么?
I have objects containing Quartz-2D references (describing colors, fill patterns, gradients and shadows) in Cocoa. I would like to implement the NSCoding
protocol in my objects and thus need to serialize those opaque Quartz-2D structures.
Possible solutions might be:
Define a set of properties in my objects that allow to setup the data structures from scratch whenever they are needed. Those can then be serialized easily. Example: Store four floats for red, green, blue, and alpha, then use
CGColorCreate
. Disadvantage: Duplication of information, thus potential consistency and (so far minor) space consumption issues. I would need to write property setters manually that recreate the Quartz structure whenever a component changes. That would bloat my code substantially.Read out the properties using Quartz functions. Example: Use
CGColorGetComponents
for colors. Disadvantage: It seems to work for colors. But there are no equivalent functions for other structures, so I don't see how this could work for things like gradients, shadings, shadows etc.Read out the properties directly from the raw, opaque structures. Disadvantage: As the documentation says, the structures are supposed to be opaque. So in case something changes under the hood, my code will break. (Apple certainly would not have provided a function like
CGColorGetComponents
if that was supposed to be done.) Furthermore, things like theCGFunctionRef
inside aCGShadingRef
would really be asking for trouble.
What is the best practice for serializing Quartz structures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不同类别的答案几乎各不相同:
TIFFRepresentation
方法。)The answer pretty much varies from one class to the next:
TIFFRepresentation
method.)