Easy or efficient way to copy the bitmap representation from one view to another?
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?
Very simple way, but with some limitation (see renderInContext description )
#import <QuartzCore/CALayer.h>
...
UIGraphicsBeginImageContext(anViewA.frame.size);
[anViewA.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * anImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
anViewB.image = anImage;
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.
精彩评论