title: NSImage to NSBitmapImageRep
body: <p>How to convert NSImage to NSBitmapImageRep? I have code:</p>
  
  <pre><code>- (NSBitmapImageRep *)bitmapImageRepresentation
  {
      NSBitmapImageRep *ret = (NSBitmapImageRep *)[self representations];
  
      if(![ret isKindOfClass:[NSBitmapImageRep class]])
      {
          ret = nil;
          for(NSBitmapImageRep *rep in [self representations])
              if([rep isKindOfClass:[NSBitmapImageRep class]])
              {
                  ret = rep;
                  break;
              }
      }
  
      if(ret == nil)
      {
          NSSize size = [self size];
  
          size_t width         = size.width;
          size_t height        = size.height;
          size_t bitsPerComp   = 32;
          size_t bytesPerPixel = (bitsPerComp / CHAR_BIT) * 4;
          size_t bytesPerRow   = bytesPerPixel * width;
          size_t totalBytes    = height * bytesPerRow;
  
          NSMutableData *data = [NSMutableData dataWithBytesNoCopy:calloc(totalBytes, 1) length:totalBytes freeWhenDone:YES];
  
          CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
  
          CGContextRef ctx = CGBitmapContextCreate([data mutableBytes], width, height, bitsPerComp, bytesPerRow, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), kCGBitmapFloatComponents | kCGImageAlphaPremultipliedLast);
  
          if(ctx != NULL)
          {
              [NSGraphicsContext saveGraphicsState];
              [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:[self isFlipped]]];
  
              [self drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
  
              [NSGraphicsContext restoreGraphicsState];
  
              CGImageRef img = CGBitmapContextCreateImage(ctx);
  
              ret = [[NSBitmapImageRep alloc] initWithCGImage:img];
              [self addRepresentation:ret];
  
              CFRelease(img);
              CFRelease(space);
  
              CGContextRelease(ctx);
          }
      }
  
  
      return ret;
  }
  </code></pre>
  
  <p>It works, but it causes memory leaks. At least when I am using it with ARC. Using <code>initWithData:[nsimagename TIFFRepresentation]</code> it not working correctly. Some images`s representations are not good. I think it depends on format and colorspace of image. Is there any other ways to achieve that?</p>
  
  <hr>
  
  <p>Result with <strong><em>mrwalker</em></strong> suggested solution:</p>
  
  <p>Original image:<br>
  <img src="https://i.stack.imgur.com/q8thT.png" alt="enter image description here"></p>
  
  <p>Converted to bitmapimagerep and back to image 1 time:<br>
  <img src="https://i.stack.imgur.com/5JT5l.png" alt="enter image description here"></p>
  
  <p>Converted to bitmapimagerep and back to image 3 times:<br>
  <img src="https://i.stack.imgur.com/VnMOc.png" alt="enter image description here"></p>
  
  <p>As you see image gets darker every time after converting to NSBitmapImageRep</p>