title: OpenGL ES on iOS texture loading - how do i get from a RGBA8888 .png file to a RGB565 texture?
body: <p>so i'm working with a bunch of 2048x2048 sprite sheets which fill up memory rather quickly. As is, i'm using the following method (via Ray Wenderlich) to load a texture: </p>
  
  <pre><code>- (GLuint)setupTexture:(NSString *)fileName 
  {    
      CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
      if (!spriteImage) 
      {
          NSLog(@"Failed to load image %@", fileName);
          exit(1);
      }
      size_t width = CGImageGetWidth(spriteImage);
      size_t height = CGImageGetHeight(spriteImage);
      GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));
      CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8,     width*4,CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);    
      CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
      CGContextRelease(spriteContext);
      GLuint texName;
      glGenTextures(1, &amp;texName);
      glBindTexture(GL_TEXTURE_2D, texName);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
      GLenum err = glGetError();
      if (err != GL_NO_ERROR)
          NSLog(@"Error uploading texture. glError: 0x%04X", err);    
      free(spriteData);        
      return texName;    
  }
  </code></pre>
  
  <p>so my question is, how do i discard alpha information from the CGImage, then "downsample" the image to fewer bits per pixel, and finally tell OpenGL about it?</p>