How to convert NSImage to NSBitmapImageRep? I have code:
- (NSBitmapImageRep *)bitmapImageRepresentation
{
NSBitmapImageRep *ret = (NSBitmapImageRep *)[self
You could try this method adapted from Mike Ash's "Obtaining and Interpreting Image Data" blog post:
- (NSBitmapImageRep *)bitmapImageRepresentation {
int width = [self size].width;
int height = [self size].height;
if(width < 1 || height < 1)
return nil;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: width
pixelsHigh: height
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: width * 4
bitsPerPixel: 32];
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep: rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: ctx];
[self drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0];
[ctx flushGraphics];
[NSGraphicsContext restoreGraphicsState];
return [rep autorelease];
}