title: What's wrong with my CGBitmapContext?
body: <p>I having trouble creating a valid CGBitmapContext in python. It's just returning null values, which then causes everything else to complain about the lack of definition and python crashes. I've tried setting the memory allocation to None, which means it's supposed to sort itself out, but that doesn't work either. And I don't think the objC buffer is being allocated either. Any help would be greatly appreciated.</p>
  
  <pre><code>#!/usr/bin/python
  
  import os, sys, objc
  from Quartz import *
  
  os.environ["CG_CONTEXT_SHOW_BACKTRACE"] = '1'
  resolution = 300 #dpi
  scale = resolution/72
  
  cs = CGColorSpaceCreateWithName(kCGColorSpaceSRGB)
  # Options might be: kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedLast \ or FIRST
  transparency = kCGImageAlphaNoneSkipLast
  
  #Save image to file
  def writeImage (image, url, type, options):
      destination = CGImageDestinationCreateWithURL(url, type, 1, None)
      CGImageDestinationAddImage(destination, image, options)
      CGImageDestinationFinalize(destination)
      CFRelease(destination)
      return
  
  if __name__ == '__main__':
  
      for filename in sys.argv[1:]:
          pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(filename))
          numPages = CGPDFDocumentGetNumberOfPages(pdf)
          shortName = os.path.splitext(filename)[0]
  
          # For each page, create a file
          for i in range (1, numPages+1):
              page = CGPDFDocumentGetPage(pdf, i)
              if page:
          #Get mediabox
                  mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
                  x = CGRectGetWidth(mediaBox)
                  y = CGRectGetHeight(mediaBox)
                  x *= scale
                  y *= scale
          # Allocate Memory, in this day and age.
                  try:
                      rasterData = objc.allocateBuffer(int(4 * x * y))
                  except  MemoryError: break
          # Create a Bitmap Context
                  ctx = CGBitmapContextCreate(rasterData, x, y, 8, x, cs, transparency)
                  CGContextSaveGState (ctx)
                  CGContextScaleCTM(ctx, scale,scale)
                  CGContextDrawPDFPage(ctx, page)
                  CGContextRestoreGState(ctx)
          # Convert to an "Image"
                  image = CGBitmapContextCreateImage(ctx) 
          # Create unique filename per page
                  outFile = shortName + str(i) + ".tiff"
                  url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outFile, len(outFile), False)
          # kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG
                  type = 'kUTTypeTIFF'
                  options = {
                      kCGImagePropertyTIFFXResolution : 300,
                      kCGImagePropertyTIFFYResolution : 300
                      }
                  writeImage (image, url, type, options)
                  CGContextRelease(ctx)
                  del page
  </code></pre>