Skip to content

Instantly share code, notes, and snippets.

@mattwymore
Last active August 29, 2015 13:57
Show Gist options
  • Save mattwymore/9416196 to your computer and use it in GitHub Desktop.
Save mattwymore/9416196 to your computer and use it in GitHub Desktop.
This allows you to read a PNG image from "path" using Core Graphics functions and then access the bytes that back the image.
//path is a plain C string with the path to your image
CGDataProviderRef sourceDataProvider = CGDataProviderCreateWithFilename(path);
CGImageRef sourceImage = CGImageCreateWithPNGDataProvider(sourceDataProvider,
NULL,
NO,
kCGRenderingIntentDefault);
CGDataProviderRelease(sourceDataProvider);
CGDataProviderRef sourceProvider = CGImageGetDataProvider(sourceImage);
size_t width = CGImageGetWidth(sourceImage);
size_t height = CGImageGetHeight(sourceImage);
CFDataRef sourceData = CGDataProviderCopyData(sourceProvider); //Release this later
CFRelease(sourceImage);
CFIndex sourceBytesCount = CFDataGetLength(sourceData);
const UInt8 *sourceBytes = CFDataGetBytePtr(sourceData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment