复制和序列化Quartz引用的最佳实践 [英] Best practice of copying and serializing Quartz references

查看:122
本文介绍了复制和序列化Quartz引用的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象包含Quartz-2D参考(描述颜色,填充图案,渐变和阴影)在Cocoa。我想在我的对象中实现 NSCoding 协议,因此需要序列化那些不透明的Quartz-2D结构。



可能的解决方案可能是:




  • 在我的对象中定义一组属性,允许从头开始设置数据结构需要。那些可以很容易地序列化。 示例:存储红色,绿色,蓝色和alpha四个浮动,然后使用 CGColorCreate 缺点:信息重复,因此潜在的一致性和(至少很少)空间消耗问题。我需要手动编写属性设置,当组件更改时重新创建Quartz结构。


  • 使用Quartz函数读出属性。 示例:使用 CGColorGetComponents 获取颜色。 缺点:似乎适用于颜色。但是没有其他结构的等效函数,所以我不知道如何在渐变,阴影,阴影等等方面起作用。


  • 属性直接来自原始,不透明结构。 缺点:如文档所述,结构应该是不透明的。所以,如果事情发生了变化,我的代码将打破。 (Apple肯定不会提供像 CGColorGetComponents 这样的函数)。此外,像 CGFunctionRef




p>

解决方案

每个类都有不同的解决方案: / p>


  • CGImage:使用CGImageDestination创建TIFF文件。 (等于NSImage的 TIFFRepresentation 方法。)

  • CGPath :写一个可以用来描述路径的元素,例如PostScript代码。

  • CGColorSpace:可以导出ICC表示。

  • > CGColor:如您所述,但不要忘记包含颜色空间。

  • CGLayer: Convoluted:
  • CGFont :该名称应该足够用于大多数应用程序。如果你真的很喜欢(即使用变体功能),你会想要包括字体的变体字典。你必须分别保持你的字体大小的知识,因为CGFont没有一个和CGContext不让你得到你设置的一个。
  • CGPDFDocument:快速查看,看起来CGPDFObjects是不可变的,因此您只需存档原始的PDF数据或您从中获取的URL。

  • CGGradient,CGPattern,CGShading和大多数其他类: Yup,你被拧了。您只需要分别保存创建对象的所有信息。


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?

解决方案

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.

这篇关于复制和序列化Quartz引用的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆