我正在尝试读取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]
,但结果是一样的。
我没有收到错误消息或警告,但我认为加载图片时出现了问题? 请帮帮我,我做错了什么?是否有更好的方法来读取像素颜色数据?
答案 0 :(得分:2)
您的错误在这里:
NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tif"];
-------------------------------------------------------------------^^^^
您可以使用:
NSImage *picture= [NSImage imageNamed:@"bais2.tiff"];
或者:
NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tiff"];
答案 1 :(得分: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);
输出:color = NSCalibratedRGBColorSpace 1 1 1 1