I can imagine the possibility when the window is in the background with no part 
of the window visible that if the system is under memory pressure (System or 
GPU) that the backing store might be discarded which would then invalidate your 
CIContext.

You could save the pointer to the CGContext at the same time as creating your 
CIContext and then in your draw rect you could get the CGContext again using 
NSGraphicsContext.currentContext.graphicsPort and see if it is different to the 
one saved.

I am not sure that this would be sensible way of keeping track of whether you 
need to update your CIContext or not but it might be useful to help work out 
what is happening.

If you image view does not resize and is not too large a possible solution 
might be to have a bitmap context the size of the image view which is where the 
CIContext draws to and then you draw that to the image view. But that might be 
slower than just recreating the CIContext each time.

Kevin

> On 27 Nov 2015, at 13:39, [email protected] wrote:
> 
> Hi,
> 
> I have an NSImageView with a custom drawing routine. It works as expected 
> most of the time, but sometimes the image is not drawn - the reason was not 
> obvious. Once when this happened, I opened a popover window and saw that 
> image drawn in its background, so it appears I’m doing something incorrect 
> with the context.
> 
> This view draws a lot, and I heard in a WWDC video that it’s a good idea to 
> cache the context as it’s “expensive” to create. It looks roughly like this:
> 
> - (CIContext*)ciContext
> {
>    if (_ciContext == nil) {
>        CGContextRef cgContext;
>        if (floor(NSAppKitVersionNumber) < NSAppKitVersionNumber10_10) {
>            // anything up to 10.9.x
>            cgContext = NSGraphicsContext.currentContext.graphicsPort;
>        } else {
>            // 10.10 or higher
>            cgContext = NSGraphicsContext.currentContext.CGContext;
>        }
> 
>        # create context
>        _ciContext = [CIContext contextWithCGContext:cgContext ...
>    }
>    return _ciContext;
> }
> 
> In drawRect:, I use CoreImage to create a CIImage, draw it, then draw an 
> overlay on top of that:
> 
> - (void)drawRect:(NSRect)dirtyRect
> {
>    // ... build image, then draw:
>    [self.ciContext drawImage:image inRect:inRect fromRect:image.extent];
> 
>    // draw overlay, obtaining the CGContextRef the same way as in the 
> CIContext above.
> }
> 
> Another data point: the image overlay is *always* drawn; the CIImage 
> sometimes is not.
> 
> Is it appropriate to cache a CIContext in this way? It feels like I’m missing 
> a lock/unlock, store/restore, or losing the context somewhere, but I’m not 
> sure exactly the correct approach.
> 
> Thanks!
> 
> Demitri
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list ([email protected])
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/ktam%40yvs.eu.com
> 
> This email sent to [email protected]


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to