• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Caching CIContext?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Caching CIContext?


  • Subject: Caching CIContext?
  • From: email@hidden
  • Date: Fri, 27 Nov 2015 08:39:14 -0500

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


  • Follow-Ups:
    • Re: Caching CIContext?
      • From: Kevin Meaney <email@hidden>
  • Prev by Date: Re: Bunch of CoreData based NSDocument questions.
  • Next by Date: Re: Caching CIContext?
  • Previous by thread: Re: Non-deprecated way to determine Process Type (LSUIElement etc.) ?
  • Next by thread: Re: Caching CIContext?
  • Index(es):
    • Date
    • Thread