macos - 尝试获取像素的颜色值

标签 macos colors pixel nsimage

我正在尝试读取 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/

相关文章:

Mac OS X 10.5.8 上 Rails 2.3.5 的 MySQL 绑定(bind)

swift - 如何在 macOS 上的 Swift3 中获取字符串的长度

android - 如何在android studio中导入colorPickerDialog库

java - 缓冲图像颜色饱和度

image - 访问图像像素值

python - Pygame 窗口在 Mac 上无法关闭的问题

java - 将颜色更改为 JPanel 并返回到原始状态

html - 移动图标 - 像素

WPF - HitTest 渲染的字符像素

php - 如何让 php 判断它是在 iOS 还是 MacOS 上运行