问题描述
我正在NSBitmapImageRep
和NSBitmapImageRep
之间切换(因为只有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