我正在尝试读取 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]];
而不是[[图片表示] objectAtIndex:0]
,但结果是相同的。
我没有收到任何错误消息或警告,但我认为加载图片时出现问题? 请帮助我,我做错了什么?有没有更好的方法来读取像素颜色数据?
最佳答案
您的错误在这里:
NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tif"];
-------------------------------------------------------------------^^^^
您可以使用:
NSImage *picture= [NSImage imageNamed:@"bais2.tiff"];
或者:
NSImage *picture = [[NSImage alloc] initWithContentsOfFile:@"bais2.tiff"];
关于macos - 尝试获取像素的颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15749550/