#include <ApplicationServices/ApplicationServices.h>
#include <stdio.h>
int main( int argc, char * const argv[] )
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
printf( "Colorspace made." );
CGContextRef context = CGBitmapContextCreate( 0,
400,
300,
8,
1600,
colorSpace,
kCGImageAlphaPremultipliedLast
);
printf( "BitmapContext made." );
CGColorSpaceRelease( colorSpace );
printf( "Colorspace released." );
//Drawing
CGContextSetRGBFillColor( context, 1, 0, 0, 1 );
CGContextFillRect( context, CGRectMake ( 0, 0, 199, 149 ) );
CGContextSetRGBFillColor( context, 0, 0, 1, 1 );
CGContextFillRect( context, CGRectMake ( 200, 150, 399, 299 ) );
printf( "Drawn Rectangles." );
//Image
CGImageRef image = CGBitmapContextCreateImage( context );
printf( "Image created." );
char *pathstr = "/Users/monkeyman/test.png";
CFStringRef path = CFStringCreateWithCString( kCFAllocatorDefault, pathstr, kCFStringEncodingUTF8 );
printf( "Path created." );
CFURLRef url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, false );
printf( "URL Created." );
CFStringRef type = kUTTypePNG;
printf( "Type Created." );
CGImageDestinationRef imageDest = CGImageDestinationCreateWithURL( url, type, 1, NULL );
printf( "Image Destination Created." );
if( imageDest == NULL )
{
printf( "imageDest was NULL." );
return 0;
}
CGImageDestinationAddImage( imageDest, image, 0 );
printf( "Image added." );
bool status = CGImageDestinationFinalize( imageDest );
printf( "Finalized." );
if( status )
printf( "status = true" );
CGImageRelease( image );
CFRelease( imageDest );
printf( "Released." );
return 0;
}