如何实现NSImage的自定义图像表示 [英] How to Implement Custom Image representation for NSImage

查看:201
本文介绍了如何实现NSImage的自定义图像表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序来绘制和处理NSimage类当前不支持的特定图像格式。在这种特定格式中,图像是1D二进制表格的形式,其前面是描述数据(数字维度,图像大小,每像素位,...)的ASCII标题。



我阅读了 Coco Drawing Guide ,可以创建新的图像表示。我是Coca和Objectiv-C编程的新手,在这个主题上,我发现了 Coco绘图指南解释很短,对我不那么有用。我现在有兴趣获得更多的细节与一些示例代码如何创建新的图像表示。此外,我特别感兴趣的是了解如何覆盖initWithData和Draw方法,Cocoa应用程序正确解释了新的图像表示。



有没有知道与我分享一个链接

如果你想要做的是绘制你的图像格式,而不是特别关心关于将它再次传回您的应用程式,您最好不要忘记子类化 NSImageRep ,只要建立一个 NSBitmapImageRep 具有与您的图像匹配的位深度和尺寸。然后请求它的 -bitmapData



这将给你一个原始缓冲区,你可以直接地址,然后逐个将图片的每个像素逐个复制到缓冲区中的适当位置。



如果您确实需要自定义 NSImageRep ,那么所有你需要做的是子类 NSImageRep ,然后在 initWithData:方法,你将图像数据存储在一个ivar,然后调用所有的各种方法来告诉图像rep应如何配置,如 -setBitsPerSample: -setPixelsHigh:等。 NSImageRep 类的文档说明了初始化图像时应调用哪些方法。 p>

要实现 -draw ,你确实需要在当前上下文中绘制像素。显然,你如何做到这一点取决于你正在绘制的图像类型(例如,一个矢量格式将有一个非常不同的绘图代码到位图)。



绘制一个位图图像将循环通过像素,并为每个像素调用 NSRectFill()一个像素大小的矩形,这是相当快,虽然不是特别有效。



但是,最好创建一个 NSBitmapImageRep 并处理位图上下文中的像素 -bitmapData )。然后可以绘制您可以从 NSBitmapImageRep 中获取的 CGImageRep 。如果您在 initWithData:方法中创建 NSBitmapImageRep ,您只需要分配像素一次。


I am developing an application to draw and process specific image format not currently supported by the NSimage class. In this specific format, the images are in form of a 1D binary table preceded by an ASCII header which describe the data (number dimension, image size, bit per pixel, ...).

I read in the Coco Drawing Guide that it is possible to create new image representation. I am new in Coca and Objectiv-C programming and, on this topic, I find the Coco Drawing Guide explanation very short and not so helpfull to me. I am now interested in getting more detail with some example code on how to create new image representation. Also I am particularly interested in understanding how to overwrite the initWithData and Draw method in order Cocoa application correctly interpret the new image representation.

Does any know share with me a link where the method is described in more detail ?

解决方案

If all you want to do is draw your image format and don't particularly care about passing it back out of your app again, you'd be best off forgetting about subclassing NSImageRep and just creating an NSBitmapImageRep with a bit depth and dimensions matching your image. You then ask it for its -bitmapData.

That will give you a raw buffer which you can directly address, and you'd then literally copy each of the pixels of your image one by one into the appropriate locations in the buffer.

If you really do want a custom NSImageRep, then all you need to do is subclass NSImageRep and then in your initWithData: method you would store the image data in an ivar and then call all the various methods to tell the image rep how it should be configured, such as -setBitsPerSample:, -setPixelsHigh: etc. The documentation for the NSImageRep class explains which methods should be called when initialising the image.

To implement -draw you literally need to draw your pixels in the current context. Obviously how you do this depends on the type of image you're drawing (e.g. a vector format will have very different drawing code to a bitmap one).

One way to draw a bitmap image would be to loop through the pixels and call NSRectFill() with a one pixel-sized rectangle for each pixel, which is pretty fast although it's not particularly efficient.

However, you'd be better off creating an NSBitmapImageRep and manipulating the pixels in the bitmap context (returned from -bitmapData) directly. You can then just draw the CGImageRep that you can get from the NSBitmapImageRep. If you create the NSBitmapImageRep in your initWithData: method, you would only have to assign the pixels once.

这篇关于如何实现NSImage的自定义图像表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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