在iOS 5中如何捕获父视图的图像

4

我使用下面的代码来捕获父视图的图像,以便在模态视图中用作背景。但是在iOS 5中它不再起作用(下面的变量“parentViewImage”为空)。有人可以帮忙吗?

// grab an image of our parent view
UIView *parentView = self.parentViewController.view;
UIGraphicsBeginImageContext(parentView.bounds.size);
[parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

+1 我也遇到了同样的问题。我把你的答案从问题中移出来,放到了回答中。你应该接受它,让人们知道它是正确的。 - Erik B
1个回答

5
在iOS 5中,您需要使用presentingViewController代替parentViewController属性。
UIView *parentView = self.presentingViewController.view;
UIGraphicsBeginImageContext(parentView.bounds.size);
[parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接