我有一个结构,其中包含一个指向数据集的指针,在本例中是一个 16 位灰度图像。我想将此数据转换为 NSImage 以便我可以显示它,然后将其保存为 .TIF 文件。手册中的路线似乎是这样的:
(Create *myNSImData from frame->image, which is a pointer)
NSImage *TestImage = [[NSImage alloc] initWithData : myNSImData];
(display TestImage, save it, whatever else)
[TestImage release];
我不知道如何创建 NSData 对象并确保它包含 16 位数据数组。尝试重铸指针会给出警告但没有数据。我可以简单地增加指针,一次将一个字节从帧->图像传输到数据对象,但我不明白如何将数组结构传递给数据对象。有任何想法吗?谢谢
使用您的建议的更多尝试我可以通过以下方式将此数据转换为 .TIF 文件:
for (uint32 row = 0 ; row < MaxHeight ; row++)
{
for (uint32 column = 0;column< MaxWidth;column++)
{
tempData = (uint8_t)*frame->image; //first byte
*frame->image++;
buf[2 * column + 1] = (unsigned char) tempData;
tempData = (uint8_t)*frame->image; //second byte
*frame->image++;
buf[2 * column] = (unsigned char) tempData;
}
TIFFWriteScanline(tiffile,buf,row,0);
}
有了这样生成的 .TIF 文件,我可以创建一个 NSImage 并显示它:
NSImage *TestImage = [[[NSImage alloc] initWithContentsOfFile:inFilePath] autorelease];
[viewWindow setImage: TestImage];
我的问题现在变成了 - 我可以创建一个可以以相同方式显示的 NSData 对象吗?我尝试了以下方法(产品是图像的高度*宽度):
NSData *ReadImage = [[[NSData alloc] initWithBytes: frame->image length:2*product] autorelease] ;
NSImage *NewImage = [[[NSImage alloc] initWithData:ReadImage] autorelease];
NSSize newSize;
newSize.height = MaxHeight; //height of the image
newSize.width = MaxWidth; //width of the image
[NewImage setSize:newSize];
[viewWindow setImage: NewImage];
当我尝试这个时,什么都没有显示。我还尝试创建一个包含数据的 uint16_t 数组,并提供指向该数据的指针 - 再次,没有任何显示。有任何想法吗?例如,我是否必须告诉 NSData 我每像素使用 2 个字节,或者类似的东西?谢谢蒙蒂·伍德