`-drawInRect` seems to be different in 10.11? What could have changed?


Edit: I got a downvote, and I just wanted to make sure it was because I didn't ask the dev forums (which I did, https://forums.developer.apple.com/thread/11593, but haven't gotten any response). This is a pretty big issue for us so I figured it'd be best to cast a wide line out, maybe somebody can help.

We have an app that works in 10.10, but has this issue in 10.11. We call -drawRect in a NSGraphicsContext on an NSImage that is properly set in both OS

But in 10.11 this NSImage doesn't get drawn.

I'm pretty novice, but I have been debugging for quite a while to get to where I'm at now, and I'm just plain stuck. Was seeing if anybody ran into this before, or has any idea why this could be.

Here is the pertinent code: (the layer object is a CGLayerRef that is passed into this method, from the -drawRect method) Here is how the layer is instantiated:

NSRect scaledRect = [Helpers scaleRect:rect byScale:[self backingScaleFactorRelativeToZoom]];  

   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; /  
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, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);  
   CGLayerRef canvasLayer = CGLayerCreateWithContext(bitmapContext, scaledRect.size, NULL);  

Here is the method that draws the image:

   CGContextRef mainLayerContext = CGLayerGetContext(layer);  
   NSRect scaledBounds = [contextInfo[kContextInfoBounds] rectValue];  
   if( !_flatCache )  
      _flatCache = CGLayerCreateWithContext(mainLayerContext, scaledBounds.size, NULL);  
      CGContextRef flatCacheCtx = CGLayerGetContext(_flatCache);  

      CGLayerRef tempLayer = CGLayerCreateWithContext(flatCacheCtx, scaledBounds.size, NULL);  
      CIImage *tempImage = [CIImage imageWithCGLayer:tempLayer];  
      CGContextRef tempLayerCtx = CGLayerGetContext(tempLayer);  

      CGContextTranslateCTM(tempLayerCtx, -scaledBounds.origin.x, -scaledBounds.origin.y);  

      [NSGraphicsContext saveGraphicsState];  
      NSGraphicsContext* newContext = [NSGraphicsContext graphicsContextWithGraphicsPort:tempLayerCtx flipped:NO];  
      [NSGraphicsContext setCurrentContext:newContext];  

      if ( [_imageController background] )  
         NSRect bgRect = { [_imageController backgroundPosition], [_imageController backgroundSize] };  
         bgRect = [Helpers scaleRect:bgRect byScale:[self backingScaleFactorRelativeToZoom]];  
         [[_imageController background] drawInRect:bgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];  

      CIImage *tempImage2 = [CIImage imageWithCGLayer:tempLayer];  

In 10.10 and 10.11, tempImage is an empty image w/ the correct size.
In 10.10 tempImage2 now has [_imageController background] properly drawn
In 10.11 tempImage2 is the same as tempImage, a blank image w/ the correct size

Unfortunately the person who originally wrote this code is gone now, and I'm too novice to go dig any lower w/o finding a book and reading it.

bgRect is not the issue, already tried modifying that around. I have also messed with the -translate arguments, but still couldn't learn anything.

Does anybody know how else I could debug this to find the issue? Or better yet, has anybody seen this issue and know what my problem is?

asked on Stack Overflow Jul 21, 2015 by A O • edited Jul 22, 2015 by A O

1 Answer


Just needed to add this line:

//`bounds` calculated from the size of the image
//mainLayerContext and flatCache defined above in question ^^
CGContextDrawLayerInRect(mainLayerContext, bounds, _flatCache);

at the very end

answered on Stack Overflow Nov 13, 2015 by A O

User contributions licensed under CC BY-SA 3.0