Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To do what you wanted, here is how you could change your code. In <code>loadImage:</code> use this line to add the <code>UIImage</code> you loaded into the <code>earth</code> layer:</p> <blockquote> <p>self.contents = image.CGImage;</p> </blockquote> <p><code>CALayers</code> have a <code>contents</code> property which you can set to a <code>CGImage</code> rather than going through the trouble of using Core Graphics calls to draw the image into the layer's context.</p> <p>Then in the parent <code>UIView</code> subclass you can add the <code>earth</code> to the view hierarchy like this:</p> <blockquote> <p>[self.layer addSublayer:earth];</p> </blockquote> <p>Note that <code>earth</code> is itself a <code>CALayer</code> with an image as its contents so I add it directly as a sublayer. The other thing I always forget to do is set the layer's <code>frame</code> property. Otherwise its width and height will be zero and so it won't show. </p> <p>In your code you created a layer but never added your layer to the view hierarchy. There is a tree if views (class UIView) and their subviews. The root of the tree is [UIWindow mainwindow] (notice that UIWindow is a subclass of <code>UIView</code>. Each <code>UIView</code> has a <code>layer</code> property of type <code>CALayer</code> which actually holds its visible content. <code>UIView</code> is just a wrapper around <code>CALayer</code> to add things like touch handling ability. </p> <p>The important idea you are missing here is that for anything to be visible on the screen it has to be part of this tree of views with a UIWindow as its root and all of the views must be visible (i.e. have their <code>hidden</code> property set to <code>NO</code>) between the toplevel window and the layer in question. Only then will a layer be visible on the screen. So somewhere in your code you need to add your layer as a sublayer of something already visible by calling <code>addSublayer:</code>.</p> <p>In your case, I'm assuming <code>earth</code> is an instance of a <code>CALayer</code> and what you have done inside the <code>loadImage:</code> method is create a new <code>CALayer</code> (with a call to <code>[CALayer layer]</code>) within the <code>earth</code> layer but then you never added that new layer to anything so it never became visible. What you did instead is attempt add the <code>earth</code> layer, which still doesn't have any content, to what I assume is a <code>UIView</code>'s layer</p> <blockquote> <p>[[self layer] addSublayer:[earth layer]]; </p> </blockquote> <p>One problem with this line is you seem to be thinking <code>layer</code> is a property of <code>earth</code>. Actually <code>layer</code> is a class method of <code>CALayer</code> which is a factory method to create new instances of <code>CALayer</code>. This is confusing because <code>[self layer]</code> actually is accessing the layer property of a <code>UIView</code> instance. (I'm assuming <code>self</code> refers to an instance of a <code>UIView</code> subclass.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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