将NSBitmapImageRep转换为NSImage

问题描述

我正在NSBitmapImageRepNSBitmapImageRep之间切换(因为只有NSImage可以让我找到每像素的颜色,但是NSImageView只能用于NSImageCell / setimage的{​​{1}})。我知道如何将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