Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you copy a CALayer?

How can you make an NSArray full of multiple instances of a CALayer (all with the same frame, contents etc)?

Background: CALayer takes a bit of overhead to create, so I would like to create a number of CALayers (all sharing the same properties) in the init method of a class (to be used later on in that class.)

like image 381
Reeds Avatar asked Mar 29 '09 16:03

Reeds


People also ask

How do I copy and paste layers?

In the Layers panel of the source image, select the layer that you want to copy. Do one of the following: Choose Select > All to select all of the pixels in the layer, and choose Edit > Copy. Then make the destination image active, and choose Edit > Paste.

How copy layers paint?

Select the layer from the [Layer] palette you want to duplicate. Select the [Layer] menu > [Duplicate Layer] to duplicate it.

What is a Calayer?

An object that manages image-based content and allows you to perform animations on that content.


2 Answers

CALayer doesn't have a built in -(id)copy method. I'm not sure why. It's not difficult to gin up your own however. Create a CALayer category and write your own copy method. All you have to do is instantiate and manually get the public ivars/properties from the original and set to the new copy. Don't forget to call [super copy]

BTW, CALayer is an object. You can add it to an NSArray.

like image 164
amattn Avatar answered Oct 20 '22 20:10

amattn


I haven't tried this with CALayer specifically, but I know you can perform a deep-copy by taking advantage of NSCoding:

CALayer *layer = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:layer]];

I'm not sure how copying them would really help with performance, though.

like image 22
MaxGabriel Avatar answered Oct 20 '22 20:10

MaxGabriel