Objective c 将NSBitmapImageRep转换为NSImage

Objective c 将NSBitmapImageRep转换为NSImage,objective-c,cocoa,nsimage,nsbitmapimagerep,Objective C,Cocoa,Nsimage,Nsbitmapimagerep,我在NSImage和NSBitmapImageRep之间切换(因为只有NSBitmapImageRep允许我查找每个像素的替换颜色,但只有NSImages可以在NSImageView/NSImageCell的setImage中使用)。我知道如何将NSImage转换为NSBitmapImageRep(使用bitmap=[NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]),但我不能做相反的事情,因为TIFFRepresenta

我在
NSImage
NSBitmapImageRep
之间切换(因为只有
NSBitmapImageRep
允许我查找每个像素的替换颜色,但只有
NSImage
s可以在
NSImageView
/
NSImageCell
setImage
中使用)。我知道如何将
NSImage
转换为
NSBitmapImageRep
(使用
bitmap=[NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]
),但我不能做相反的事情,因为
TIFFRepresentation
是只读的。那么,如何将
NSBitmapRep
转换为
NSImage

带着希望


radzo73

要从修改后的NSBitmapImageRep中恢复NSImage,我所要做的就是调用一个方法:

NSImage *image = [[NSImage alloc] initWithSize:someSize];

[...]

NSBitmapImageRep *rawImage = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];

[code that gets and sets pixels]

[image initWithCGImage:[rawImage CGImage] size:image.size]; // overwrite original NSImage with modified one

要从修改后的NSBitmapImageRep中恢复NSImage,我所要做的就是调用一个方法:

NSImage *image = [[NSImage alloc] initWithSize:someSize];

[...]

NSBitmapImageRep *rawImage = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];

[code that gets and sets pixels]

[image initWithCGImage:[rawImage CGImage] size:image.size]; // overwrite original NSImage with modified one

这回答了你的问题吗?要么没有,要么我做错了。我正在使用我的
NSBitmapImageRep
的大小和表示方式创建一个新的
NSImage
,但在显示它时,我得到的只是一个空白图像:/您必须非常小心地设置
initWithBitmapDataPlanes:…
。有很多(有时令人困惑)参数,微小的错误可能不会导致任何结果,但是
NSBitmapImageRep
确实有效。我已经在各种各样的图片上用了很多年了。尝试手动构建一个简单的8x8图像缓冲区,并将其转换为图像。这是否回答了您的问题?要么没有,要么我做错了。我正在使用我的
NSBitmapImageRep
的大小和表示方式创建一个新的
NSImage
,但在显示它时,我得到的只是一个空白图像:/您必须非常小心地设置
initWithBitmapDataPlanes:…
。有很多(有时令人困惑)参数,微小的错误可能不会导致任何结果,但是
NSBitmapImageRep
确实有效。我已经在各种各样的图片上用了很多年了。尝试手动构建一个简单的8x8图像缓冲区,并将其转换为图像。