Converting an NSBitmapImageRep to NSImage

990 views Asked by At

I'm flipping between NSImage and NSBitmapImageRep(because only NSBitmapImageRep lets me find-replace colors per-pixel, but only NSImages can be used in a NSImageView/NSImageCell's setImage). I know how to convert a NSImage to a NSBitmapImageRep (using bitmap = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]), but I can't do the opposite of that because TIFFRepresentation is read-only. So, how do I convert a NSBitmapRep to a NSImage?

With hope,

radzo73

1

There are 1 answers

0
user1134918 On

All that I had to do to get back an NSImage from my modified NSBitmapImageRep was call one method:

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