Objective c 由两个UIView组成的UIView的快照在完成时消失

Objective c 由两个UIView组成的UIView的快照在完成时消失,objective-c,uiview,uiviewcontroller,snapshot,Objective C,Uiview,Uiviewcontroller,Snapshot,在myUIViewController中,我添加了一些UIView,我想拍摄一个自定义UIView的快照,它由一些UIView组成,我这样做: UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height+second.frame.size.height)]; [tmp addSubview:first]; CGRect

在my
UIViewController
中,我添加了一些
UIView
,我想拍摄一个自定义
UIView
的快照,它由一些
UIView
组成,我这样做:

UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height+second.frame.size.height)];

    [tmp addSubview:first];

    CGRect frame = second.frame;
    frame.origin.y = CGRectGetMaxY(first.frame);
    [second setFrame:frame];

    [tmp addSubview:second];

    UIGraphicsBeginImageContextWithOptions(tmp.bounds.size, YES, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [tmp.layer renderInContext:context];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
编辑并添加更多详细信息:

UIView *first = self.myFirstView;
UIView *second = self.mySecondView;
self.myFirstView
self.mySecondView
是UIView与Interface Builder连接的:

@property (weak, nonatomic) IBOutlet UIView *myFirstView;
@property (weak, nonatomic) IBOutlet UIView *mySecondView;

图像创建得非常完美,但当完成创建后,
UIViewController
中的
UIView
就消失了,我该如何解决这个问题呢?

还不清楚你在问什么。您可以显示如何添加视图的代码吗?当然,它会消失,
tmp
视图不会添加到任何superview中,因此它不会被渲染,因为一旦超出范围,它可能会被释放(ARC创造奇迹:))。无论如何,请向我们展示如何将tmp添加到视图层次结构中。tmp是在那里创建的,正如您在上面的代码中所看到的,第一个和第二个UIView是作为IBOutlet与xibi连接的。我已在我的问题中添加了更多详细信息尝试将您的
tmp
视图添加到超级视图中(如果您不想让用户看到它,请在屏幕外),然后创建一个快照。这应该行得通。另外,您起诉
renderInContext:
而不是
drawViewHierarchyInRect:afterScreenUpdates:
的具体原因是什么?