// UIScrollView全体のスナップショット
// https://gist.github.com/thestoics/1204051
extension UIScrollView {
public var snapshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(contentSize, false, 0)
defer {
UIGraphicsEndImageContext()
}
guard let context = UIGraphicsGetCurrentContext() else { return nil }
let previousFrame = frame
frame = CGRect(origin: frame.origin, size: contentSize)
layer.render(in: context)
frame = previousFrame
return UIGraphicsGetImageFromCurrentImageContext()
}
}
// navigationBarのスクリーンショット
let rect = self.navigationController!.navigationBar.frame
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
defer {
UIGraphicsEndImageContext()
}
guard let context: CGContext = UIGraphicsGetCurrentContext() else { return nil }
self.navigationController!.navigationBar.layer.render(in: context)
let capturedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
return capturedImage