Creating and Displaying Bitmap in CGContextRef
Creating and Displaying Bitmap in CGContextRef
- Subject: Creating and Displaying Bitmap in CGContextRef
- From: Mario Gajardo Tassara <email@hidden>
- Date: Wed, 20 Feb 2008 12:19:41 -0300
Hi to all,
Im in the process of learning Quartz 2D programming API, and im trying
to display a tiled image in a NSView, i wrote this code for creating a
valid bitmap context and a routine for displaying a tiled image, but
nothing is displayed, im not sure what`s wrong because my printf
debugging statements show good info about the creation of the
CGcontext and the loading of the image, any help will be appreciated,
thanks:
-(CGContextRef)CGCreateImageContext:(int)pixelsWidth pixelsHeight:
(int)pixelsHeight {
CGContextRef theContext=NULL;
CGColorSpaceRef colorSpace;
void *bitmapData;
int bitmapByteCount;
int bitmapBytePerRow;
bitmapBytePerRow = (pixelsWidth * 4);
bitmapByteCount = (bitmapBytePerRow * pixelsHeight);
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
bitmapData = malloc(bitmapByteCount);
if(bitmapData == NULL) {
printf("Error");
return NULL;
}
theContext = CGBitmapContextCreate(bitmapData,
pixelsWidth,
pixelsHeight,
8,
bitmapBytePerRow,
colorSpace,
kCGImageAlphaNoneSkipLast);
if(theContext == NULL) {
free(bitmapData);
printf("ERROR");
return NULL;
}
printf("Context bits per component = %d\nContext bits per pixel = %d
\n",
CGBitmapContextGetBitsPerComponent(theContext),
CGBitmapContextGetBitsPerPixel(theContext));
CGColorSpaceRelease(colorSpace);
return theContext;
}
-(void)CGDrawImage:(NSRect)rect {
char *filename={"/Users/mario/Pictures/AVATARS/xxx.jpg"};
int imageWidth,imageHeight;
CGDataProviderRef CGdataProv =
CGDataProviderCreateWithFilename(filename);
CGImageRef imageRef =
CGImageCreateWithJPEGDataProvider
(CGdataProv,NULL,YES,kCGRenderingIntentDefault);
imageWidth=CGImageGetWidth(imageRef);
imageHeight=CGImageGetHeight(imageRef);
printf("size w = %d\n",imageWidth);
printf("size h = %d\n",imageHeight);
CGRect theCGRect=CGRectMake(0,0,imageWidth,imageHeight);
int w=rect.size.width;
int h=rect.size.height;
CGContextRef theBitmapContext = [self CGCreateImageContext:w
pixelsHeight:h];
if(!CGdataProv) {
printf("CGdataProvider error \n");
}
if(!imageRef) {
printf("CGImage error");
}
CGFloat r,g,b;
r=(drand48());
g=(drand48());
b=(drand48());
CGContextDrawTiledImage(theBitmapContext,theCGRect,imageRef);
char *bitmapData = CGBitmapContextGetData(theBitmapContext);
CGContextRelease(theBitmapContext);
if(bitmapData) {
free(bitmapData);
}
CGImageRelease(imageRef);
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden