问题描述
我有anViewA和anViewB。它们是带有很多子视图的纯UIView对象。我需要的只是位图表示或让我们说anViewA的图像复制到anViewB,所以anViewB看起来像anViewA(没有subView像anViewA)。
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).
第一我试图使用-copy消息,但不幸的是UIView不符合NSCopying协议。
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?
推荐答案
很简单的方法,但有一些限制(见 description)
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;
这篇关于容易或有效的方式将位图表示从一个视图复制到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!