Haentz
Haentz

Reputation: 417

Possible to copy CALayer from UIView?

Here's my setup: I have a CALAyer to which I want to add sublayers. I create these sublayers by setting upa UILabel and then adding the UILables layer to my main layer. Of course this leaves the heavy UILabel object hovering around in the background. Is it possible to get the layer with all its content from a UIView and get rid of the UIView itself? I already tried this:

UILabel* label;
[...]
[mainLayer addSublayer:[label.layer copy]];
[label release];

But whenever I release the UIView, the content of the layer is also removed. Is this even possible or does the UIView's layer always need the UIView itself to show its content? I thought of the layer as a kind of canvas, to which the UIView paints. I guess I could be wrong with this assumption :)

Upvotes: 6

Views: 8339

Answers (6)

Mark A. Donohoe
Mark A. Donohoe

Reputation: 30328

UIViews are not that heavy in iOS. For the most part, you can think of them as a supporting wrapper around a CALayer. Unlike on Mac where a NSView doesn't have to be backed by a CALayer, that's not true in iOS. All UIView instances are CALayer-backed and that's where most of the heavy lifting is. Apple's docs even say things like 'Don't bother animating a UIView's layer. Animate the UIView directly as it's essentially just sending it all down to the layer anyway.' (Paraphrased, of course.)

Point being, unless you specifically need UILayer instances for something that's not directly supported by a UIView, just stick with working with UIViews entirely and you should be good to go.

Upvotes: 1

ossamacpp
ossamacpp

Reputation: 686

use CAReplicatorLayer, you will be able to completely replicate that layer

Upvotes: 0

Zev Eisenberg
Zev Eisenberg

Reputation: 8148

You can also circumvent UILabel entirely and create your text with a CATextLayer.

Upvotes: 0

U62
U62

Reputation: 4363

UILabel really isn't a very complex control. I suggest you give up this line of attack and learn how to draw what you want using Quartz into a fresh CGImage. You can then assign this to the contents property of the CALayer without having to worry about all this other stuff.

Upvotes: 0

Corey Floyd
Corey Floyd

Reputation: 25969

I can't understand why you wouldn't be able to copy a layer. True a layer is an "integral part" of a UIView. But in the end it is just another object with several properties.

Actually there is a method for CALayer called:

- (id)initWithLayer:(id)layer

But it isn't intended to make a copy of a layer. (You can read Apple's docs for the reasoning why)

CALayer does not conform to NSCopying, so you have two options:

  1. Subclass it and implement "copyWithZone:" (and conform to NSCopying)
  2. Write a method/function that will return a "copy" of a CALayer

Regardless of which way you choose, the question you have to ask is: Which properties of the CALlayer do you want to copy?

Let's say you want to copy just the contents and frame:

CALayer copyLayer = [CALayer layer];

copyLayer.contents = theLayerToBeCopied.contents;
copyLayer.frame = theLayerToBeCopied.frame;

return copyLayer;

You could go through and copy every property of the layer, or just copy the ones you need. Might be a good method to put in a CALayer category.

Upvotes: 4

Massimo Cafaro
Massimo Cafaro

Reputation: 25419

It is not possible: a layer is a property of a UIView. Therefore, when you release the UIView, its layer is gone. Your idea of thinking of the layer as a kind of canvas is not wrong. But, the layer is an integral part of its UIView.

Upvotes: 1

Related Questions