views:

30

answers:

2

I have anViewA and anViewB. They're plain UIView objects with a lot of subviews. I need just the bitmap representation or lets say the "image" of anViewA copied over to anViewB, so that anViewB looks like anViewA (without having subviews like anViewA).

First I tried to use the -copy message, but unfortunately UIView doesn't conform to NSCopying protocol.

Is there another trick to make a "visual copy" of a view?

+1  A: 

You need to draw the CGLayer of each subview of A into the single CGLayer of view B.

Start with the CGLayer of A, draw it to B, then walk down the view hierarchy repeating the draw until you've drawn everything into B's CGLayer. You will end up with a picture in B.

TechZen
+1  A: 

Very simple way, but with some limitation (see renderInContext description )

UIGraphicsBeginImageContext(anViewA.frame.size);
[anViewA.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * anImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
anViewB.image = anImage;
Vladimir