title: How can I obtain clean alpha channel information from an PNG image in iPhone OS?
body: <p>I have created an context like this (simplified):</p>
  
  <pre><code>CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  
  CGContextRef context = CGBitmapContextCreate (bitmapData,
    pixWide,
    pixHeigh,
    8, // bits per component
    bitmapBytesPerRow,
    colorSpace,
    kCGImageAlphaPremultipliedFirst);
  </code></pre>
  
  <p>Now, when I try to extract the data for the first pixel in my PNG with Alphatransparency, it has very weird alpha values. I have an simple PNG that's a square. On each edge I cut off 10x10 pixel and made them totally transparent. Alpha shouldn't be something like 153 there.</p>
  
  <p>There's an kCGImageAlphaOnly declared in CGImage.h. The doc says: </p>
  
  <blockquote>
    <p>kCGImageAlphaOnly There is no color
    data, only an alpha channel.</p>
  </blockquote>
  
  <p>Ok, so that actually sounds good, because I only need Alpha data, and nothing else. But this raises some question marks in my head. If I do habe a fully equipped PNG with a bunch of colors + alpha: Would this constant make sure that my PNG is converted to match that color space? Or would I have to provide an PNG that matches that specified color space?</p>
  
  <p>Edit: I tried using kCGImageAlphaOnly, but I get this error:</p>
  
  <pre><code>&lt;Error&gt;: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 0-component colorspace; kCGImageAlphaOnly; 55 bytes/row.
  </code></pre>
  
  <p>What may be the problem here? I specified this before:</p>
  
  <pre><code>size_t pixelsWide = CGImageGetWidth(inImage);
  size_t pixelsHigh = CGImageGetHeight(inImage);
  bitmapBytesPerRow = (pixelsWide * 1); // not * 4, because I just want alpha
  bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
  </code></pre>
  
  <p>Edit: I've been reading this a minute ago:</p>
  
  <blockquote>
    <p>PNG's which are added to XCode are
    optimized by 'pngcrush' during
    compilation. This does some
    byte-swapping (from RGBA to BRGA) and
    pre-multiplication of alpha.</p>
  </blockquote>
  
  <p>I assume that this pre-multiplication of alpha makes trouble.</p>
  
  <p>Edit: The alpha channel keeps intact after pngcrunch did the byte-swapping stuff to the PNG. Since I don't care about colors, just alpha, that pre-multiplication shouldn't be a too big problem, I think.</p>
  
  <p>My PNG's have been 24bit PNG bevor I added them to Xcode.</p>