Note that there are some explanatory texts on larger screens.

plurals
  1. POAnnotating over an NSImageView with CALayers, layer-hosting or layer-backed?
    text
    copied!<p>I would like to an annotate over an <code>NSImageView</code> with <code>CALayer</code>. The purpose is to be able to draw a shape (a <code>CALayer</code>) over the image, and in a separate view display information about the area that has been highlighted. For example, the user could draw a rectangle over the image view and the pixels under this area could be magnified and displayed in another window. </p> <p>I would like some general advice on the best way to implement drawing over the top of an image view using <code>CALayer</code>. If the view is resized I want the position of the selection area to stay synchronised, that is, the position relative to the image should remain constant.</p> <p>I'm slightly confused because the CALayer documentation states for <strong>layer-backed views</strong>,</p> <ul> <li><p><em>"When using layer-backed views you should never interact directly with the layer. Instead you must use the standard view programming practices."</em></p> <p>Clearly this is not the appropriate choice because I want to own the layer and interact with it directly.</p></li> </ul> <p>For <strong>layer-hosting views</strong>,</p> <ul> <li><p><em>"When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view."</em></p> <p>So it would seem the view should be layer-hosting. However, making my NSImageView (subclass) layer-hosted prevents the image from drawing because <code>NSImageView</code> relies (I assume) on <code>-drawRect:</code>. <code>-drawRect:</code>is not called with layer-hosting views. It also wish to do some custom drawing in the view when it is empty e.g. a placeholder message, for that I need <code>-drawRect:</code>.</p></li> </ul> <p>So one possible solution would be in Interface Builder place an "annotation view" above the image view, i.e. they are not nested but simply occupying the same space and have the same size. I can specify the z ordering in Interface Builder so the annotation view is always on top. I then make the annotation view be layer-hosting, and leave the image view unchanged.</p> <p>This seems like a hack, so I'm sure there is a better way. Of course I could draw the annotations in the image views <code>-drawRect:</code> but this would add lots of code and I dont want to do that right away without exploring alternatives.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload