方向更改后的上一个UIViewController的屏幕截图

时间:2015-06-19 06:34:10

标签: ios swift

我完全实现了这篇文章中描述的内容: Send Screenshot of ViewController to other ViewController

但是在'ProfileViewController'中更改了方向后我遇到了问题。我试图获得'ViewController'的截图,但我只是获得了之前方向的视图图像。

我尝试过这样的事情,但似乎没有任何帮助:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animateAlongsideTransition(nil, completion: { (context : UIViewControllerTransitionCoordinatorContext!) -> Void in
        var viewController = self.navigationController!.viewControllers[0] as! UIViewController

        viewController.view.setNeedsDisplay()
        viewController.view.setNeedsLayout()
        viewController.view.layoutIfNeeded()

        self.backgroundImage = viewController.getImageDump()
    })

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

'getImageDump'是UIViewController上的扩展方法:

func getImageDump() -> UIImage {
    var size = view.bounds.size
    UIGraphicsBeginImageContextWithOptions(size, true, 0);
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image
}

编辑:已经修好了;只需手动设置viewcontroller的大小。而不是

        viewController.view.setNeedsDisplay()
        viewController.view.setNeedsLayout()
        viewController.view.layoutIfNeeded()

只是做

        viewController.view.frame.size = size

2 个答案:

答案 0 :(得分:0)

使用快照

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;

答案 1 :(得分:0)

试试这个,

更改

    var size = view.bounds.size

var size = view.frame.size