0

I'm trying to show a live visual of what another NSView looks like.

My problem is not rendering the view to an image. cocoa: how to render view to image?


Let's call the source view, Incident View, and the one to many destination views, Mirror Views.

Every view has an export and import CGLayer.

The export layer will let it act as an Incident View.

The import layer will let it act as an Mirror View.

The idea is that Incident View will cast an ncdntReflection and all of its Mirror Views will catch that reflection on their mrrSurface.

  • ncdntReflection: Incident view's export CGLayer.
  • mrrSurface: Mirror view's import CGLayer.

I've tried 3 approaches so far...

1. CGLayer as backing store:

  1. Draw contents of Incident View and its subviews into ncdntReflection.
  2. Draw ncdntReflection into Incident View's current context.
  3. Set mrrSurface of Mirror View(s) to ncdntReflection.

2. Composite CALayers into CGLayer

  1. Draw backing CALayers of Incident View and its subviews into ncdntReflection.
  2. Set mrrSurface of Mirror View(s) to ncdntReflection.

3. cacheDisplayInRect or CGWindowListCreateImage

  1. Capture Incident View's contents using either method
  2. Draw those contents into ncdntReflection.
  3. Set mrrSurface of Mirror View(s) to ncdntReflection.

I didn't like any of these approaches for the following reasons.

Collapsing Layers is not an option, as I need animations on each layer. This means that options 1 and 2 will both have to be recursive to get all of the subviews. I'm worried about the performance hit for this kind of drawing.

Option 3 just takes way too long to be a live solution.

Ideally both the Incident View and Mirror Views would follow the same rendering instructions - like a WPF Visual Brush.

Is there a metal solution?

Community
  • 1
  • 1
Ping Titan
  • 67
  • 1
  • 6
  • Metal can't help you with this at all. Have you looked at `NSView`s [displayRectIgnoringOpacity:inContext:](https://developer.apple.com/reference/appkit/nsview/1483436-displayrectignoringopacity) method? – Silvan Mosberger Aug 01 '16 at 00:01
  • @SilvanMosberger That's exactly what I needed! If you answer the question I'll be sure to credit you for it. – Ping Titan Aug 02 '16 at 14:08

1 Answers1

1

Probably you're looking for NSViews displayRectIgnoringOpacity:inContext: method. It may not be the most efficient method, since it draws the view twice, but it seems to work in your case. On the other hand I'm not sure if it'd be faster to cache the pixel buffer, which requires more memory for sure anyways.

Silvan Mosberger
  • 1,786
  • 14
  • 19