Objective-C:将浮点数组显示为图像

Objective-C:将浮点数组显示为图像,第1张

概述在 Cocoa应用程序中,我想在NS ImageView中显示一个2d浮点数组.为了使代码尽可能简单,首先将数据从float转换为NSData: // dataArray: an Nx by Ny array of floatsNSMutableData *nsdata = [NSMutableData dataWithCapacity:0];long numPixels = Nx*Ny;f 在 Cocoa应用程序中,我想在NS ImageVIEw中显示一个2d浮点数组.为了使代码尽可能简单,首先将数据从float转换为NSData:

// dataArray: an Nx by Ny array of floatsNSMutableData *nsdata = [NSMutableData dataWithCapacity:0];long numPixels = Nx*Ny;for (int i = 0; i < numPixels; i++) {    [nsdata appendBytes:&dataArray[i] length:sizeof(float)];}

现在尝试显示数据(显示为空白):

[theNSImageVIEw setimage:[[NSImage alloc] initWithData:nsdata]];

这是正确的方法吗?首先需要CGContext吗?我希望用NSData实现这一目标.

我已经注意到早期的Stack帖子:32 bit data,close but in reverse,almost worked but no NSData,color image data here,但是对于这些工作的变化并没有多少运气.谢谢你的任何建议.

解决方法 您可以使用NSBitmAPImageRep来构建NSImage float-by-float.

有趣的是,其中一个初始化程序在Cocoa中具有最长的方法名称:

- (ID)initWithBitmapDataPlanes:(unsigned char **)planes                     pixelsWIDe:(NSInteger)wIDth                     pixelsHigh:(NSInteger)height                  bitsPerSample:(NSInteger)bps                samplesPerPixel:(NSInteger)spp                       hasAlpha:(BOol)Alpha                       isPlanar:(BOol)isPlanar                 colorSpacename:(Nsstring *)colorSpacename                   bitmapformat:(NSBitmapformat)bitmapformat                    bytesPerRow:(NSInteger)rowBytes                   bitsPerPixel:(NSInteger)

至少它已有详细记录.通过在平面中提​​供浮点数组来构建它之后,您就可以将NSImage放入视图中:

NSImage *image = [[NSImage alloc] initWithCGImage:[bitmAPImageRep CGImage] size:NSMakeSize(wIDth,height)];

或者,稍微清洁一点

NSImage *image = [[[NSImage alloc] init] autorelease];[im addRepresentation:bitmAPImageRep];

有一个初始化器只使用NSData容器:

+ (ID)imageRepWithData:(NSData *)bitmapData

虽然这取决于你的bitmapData包含一个正确的位图格式.

总结

以上是内存溢出为你收集整理的Objective-C:将浮点数组显示为图像全部内容,希望文章能够帮你解决Objective-C:将浮点数组显示为图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1229903.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存