Skip to content

Instantly share code, notes, and snippets.

@foobit
Created April 30, 2012 15:38
Show Gist options
  • Save foobit/2559395 to your computer and use it in GitHub Desktop.
Save foobit/2559395 to your computer and use it in GitHub Desktop.
UIImage/NSImage to raw buffer
NSString* file = [[NSString alloc] initWithUTF8String:pth.c_str()];
CGImageRef image = [UIImage imageWithContentsOfFile:file].CGImage;
[file release];
m_width = CGImageGetWidth(image);
m_height = CGImageGetHeight(image);
m_poww = std::max<int>(math::high_pow2(m_width), 16);
m_powh = std::max<int>(math::high_pow2(m_height), 16);
int pitch = m_poww * 4;
int size = m_powh * pitch;
byte* raw_data = new byte[size];
CGContextRef context = CGBitmapContextCreate(raw_data, m_poww, m_powh, 8, pitch, CGImageGetColorSpace(image), kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
CGRect rect = CGRectMake(0.0, 0.0, m_width, m_height);
CGContextTranslateCTM(context, 0, m_powh - m_height);
CGContextClearRect(context, rect);
CGContextDrawImage(context, rect, image);
CGContextRelease(context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment