2016-11-30 3 views
0

MyFirstViewController가 표시되는 동안 MySecondViewController.view의 스크린 샷을 가져 오려고합니다. MySecondViewController가 언제든지 화면에 나타나기를 원하지 않습니다. 그게 가능합니까? 내가 사용하고있는 UIView 확장 여기화면이 표시되지 않는 스크린 샷

func createPreview() { 
    let mySecondViewController = self.storyboard!.instantiateViewController(withIdentifier: "MySecondViewController") 
    self.navigationController?.pushViewController(mySecondViewController, animated: false) 
    let snapshot = mySecondViewController.view.snapshot() 
    self.navigationController?.popViewController(animated: false) 
} 

됩니다 : 여기

이 MyFirstViewController의 함수 안에 내 현재 코드이 코드

func snapshot() -> UIImage { 
    UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale) 
    drawHierarchy(in: self.bounds, afterScreenUpdates: true) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image! 
} 

snapshot 변수는 눈에 보이지 않는 사진이 포함되어 있습니다 좋은 차원. 보기가 탐색 컨트롤러에서 튀어 나온 후 스크린 샷을 찍는 것 같습니다.

afterScreenUpdates 값을 false로 설정하면 결과가 동일합니다. 그런 다음보기가 탐색 컨트롤러에 푸시되기 전에 스크린 샷을 찍는 것 같습니다.

답변

1

내가 찾고있는 것을 찾았습니다.

func createPreview() { 
    let mySecondViewController = self.storyboard!.instantiateViewController(withIdentifier: "MySecondViewController") 
    let snapshot = mySecondViewController.view.snapshot() 
} 

extension UIView { 
    func snapshot() -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, self.isOpaque, UIScreen.main.scale) 
     layer.render(in: UIGraphicsGetCurrentContext()!) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 
    } 
} 
: 여기

는 새로운 코드