
// 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:将浮点数组显示为图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)