CGLayerRef turns out empty only in OS X 10.11 (El Capitan)


I have an image editing application, that has been working through 10.10, but in 10.11 a bug came up

When I view a CIImage created w/ -imageWithCGLayer, it shows as an empty image (of the correct size) only in 10.11

   CGSize size =  NSSizeToCGSize(rect.size);
    size_t width = size.width;
    size_t height = size.height;
    size_t bitsPerComponent = 8;
    size_t bytesPerRow = (width * 4+ 0x0000000F) & ~0x0000000F; // 16 byte aligned is good
    size_t dataSize = bytesPerRow * height;
   void* data = calloc(1, dataSize);

    CGColorSpaceRef colorspace = [[[_imageController document] captureColorSpace] CGColorSpace];
    CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, kCGImageAlphaNone | kCGBitmapByteOrder32Host);

   CGLayerRef canvasLayer = CGLayerCreateWithContext(bitmapContext, scaledRect.size, NULL);

   [self drawCanvasInLayer:canvasLayer inRect:scaledRect];
   CIImage *test = [CIImage imageWithCGLayer:canvasLayer];

So when I view CIImage *test on 10.10, it looks precisely as I want it. On 10.11 it is a blank image of the same size. I tried looking at the API diffs for CGLayer & CIImage but the documentation is too dense for me. Has anybody else stumbled across this issue? I imagine it must be something w/ the initialization of the CGContextRef, because everything else in the code is size related

asked on Stack Overflow Jul 17, 2015 by A O

1 Answer


That particular API was deprecated some time ago and completely removed in macOS 10.11. So your results are expected.

Since you already have a bitmapContext, modify your -drawCanvasInLayer: method to directly draw into the bitmap and then create the image using the bitmpap context thusly,

CGImageRef tmpCGImage = CGBitmapContextCreateImage( bitmapContext );
CIImage*   myCIImage  = [[CIImage alloc] initWithCGImage: myCIImage];

Remember to do CGImageRelease( tmpCGImage ) after you are done with your CIImage.

I recently solved this very problem and posted a sample objective-C project to work around the loss of this API.


Also, don't forget to read the header file where that API is declared. There is very often extremely useful information there (in Xcode, Command+click on the specific API)

answered on Stack Overflow Mar 17, 2017 by Jeff Szuhay • edited Mar 24, 2017 by Jeff Szuhay

User contributions licensed under CC BY-SA 3.0