Creating and Displaying Bitmap in CGContextRef

  • 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);
    }
  • On Feb 20, 2008, at 7:19 AM, Mario Gajardo Tassara wrote:

    > 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:

    Your not drawing into the window at all. You need to get the CGContext
    to draw to from the current NSGraphicsContext via:

    CGContextRef context = (CGContextRef)[[NSGraphicsContext
    currentContext] graphicsPort];

    The bitmap context stuff you have looks like a red herring.
    --
    David Duncan
    Apple DTS Animation and Printing
    <david.duncan...>
  • Thanks David and Chris, your answers help me a lot , now my app draws
    to the screen :)

    El 20-02-2008, a las 16:42, David Duncan escribió:

    > On Feb 20, 2008, at 7:19 AM, Mario Gajardo Tassara wrote:
    >
    >> 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:
    >
    >
    > Your not drawing into the window at all. You need to get the
    > CGContext to draw to from the current NSGraphicsContext via:
    >
    > CGContextRef context = (CGContextRef)[[NSGraphicsContext
    > currentContext] graphicsPort];
    >
    > The bitmap context stuff you have looks like a red herring.
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    > <david.duncan...>
    >
    >
    >