首页 > 解决方案 > 将 NSBitmapImageRep 转换为 NSImage

问题描述

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

带着希望,

拉佐73

标签: objective-ccocoansimagensbitmapimagerep

解决方案


从修改后的 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

推荐阅读