Skip to content

Instantly share code, notes, and snippets.

@ochim
Last active February 5, 2020 11:39
Show Gist options
  • Save ochim/81afa8eeb0765cf297bb5f24793db440 to your computer and use it in GitHub Desktop.
Save ochim/81afa8eeb0765cf297bb5f24793db440 to your computer and use it in GitHub Desktop.
[iOS]コードでスナップショットを撮る
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment