复制和序列化 Quartz 引用的最佳实践

发布于 2024-09-27 14:27:00 字数 771 浏览 2 评论 0原文

我在 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 the CGFunctionRef inside a CGShadingRef would really be asking for trouble.

What is the best practice for serializing Quartz structures?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2024-10-04 14:27:00

不同类别的答案几乎各不相同:

  • CGImage: 使用 CGImageDestination 制作它的 TIFF 文件。 (相当于 NSImage 的 TIFFRepresentation 方法。)
  • CGPath: 编写一个应用程序函数,可用于将路径的元素描述为 PostScript 代码。编写一个简单的解释器来走向另一个方向。
  • CGColorSpace:您可以导出 ICC 表示。
  • CGColor: 正如您所描述的,但不要忘记包括颜色空间。
  • CGLayer:复杂:创建一个位图上下文,将图层绘制到其中,并转储上下文的图像,然后将其序列化。
  • CGFont: 该名称对于大多数应用程序来说应该足够了。如果您真的很喜欢(即使用变体功能),您将需要包含字体的变体字典。您必须单独维护对字体大小的了解,因为 CGFont 没有字体大小,并且 CGContext 不允许您获取在其中设置的字体大小。
  • CGPDFDocument: 乍一看,CGPDFObject 似乎都是不可变的,因此您只需存档原始 PDF 数据或从中获取数据的 URL。
  • CGGradient、CGPattern、CGShading 和大多数其他类: 是的,你完蛋了。您只需要单独维护创建对象所用的所有信息。

The answer pretty much varies from one class to the next:

  • CGImage: Use CGImageDestination to make a TIFF file of it. (Equivalent to NSImage's TIFFRepresentation method.)
  • CGPath: Write an applier function you can use to describe the path's elements as, say, PostScript code. Write a simple interpreter to go the other direction.
  • CGColorSpace: You can export the ICC representation.
  • CGColor: As you described, but don't forget to include the color space.
  • CGLayer: Convoluted: Make a bitmap context, draw the layer into it, and dump an image of the context, then serialize that.
  • CGFont: The name should be enough for most applications. If you're being really fancy (i.e., using the variations feature), you'll want to include the font's variations dictionary. You'll have to maintain your knowledge of the font size separately, since CGFont doesn't have one and CGContext doesn't let you get the one you set in it.
  • CGPDFDocument: From a quick look, it looks like CGPDFObjects are all immutable, so you'd just archive the original PDF data or the URL you got it from.
  • CGGradient, CGPattern, CGShading, and most other classes: Yup, you're screwed. You'll just need to maintain all of the information you created the object with separately.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文