2

我正在尝试读取 tif 图像中像素的颜色值,但我无法找出正确的方法。我正在使用 OSX,我的方法如下:

NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tif"]; //file is located in resoureces folder.
NSBitmapImageRep *imageRep = [[picture representations] objectAtIndex:0];

NSColor* color = [imageRep colorAtX:10 y:10];
NSLog(@"%f %f, %f", [color redComponent], [color blueComponent], [color greenComponent]);

问题是由于某种原因,NSLog 中的记录值总是变成 0.0000000....

我也尝试过使用:

NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithData:[picture TIFFRepresentation]];

而不是,[[picture representations] objectAtIndex:0]但结果是一样的。

我没有收到错误消息或警告,但我认为加载图片时有问题?请帮助我,我做错了什么?有没有更好的方法来读取像素颜色数据?

4

2 回答 2

2

你的错误在这里:

NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tif"];
-------------------------------------------------------------------^^^^

您可以使用:

NSImage *picture= [NSImage imageNamed:@"bais2.tiff"];

或者 :

NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tiff"];
于 2013-04-01T19:22:41.480 回答
1
 NSData *someNSData = [Image TIFFRepresentation];
 NSBitmapImageRep *someNSBitmapImageRepData = [[NSBitmapImageRep alloc] initWithData:someNSData];
 NSSize imageSizee = [someNSBitmapImageRepData size];

 CGFloat y = imageSize.height - 100.0;
 NSColor* color = [someNSBitmapImageRepData colorAtX:100.0 y:y];

 NSLog(@"color = %@",color);

输出:颜色 = NSCalibratedRGBColorSpace 1 1 1 1

于 2016-01-21T03:57:31.923 回答