Skip to content

Instantly share code, notes, and snippets.

@DaveBatton
Last active December 10, 2015 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveBatton/86f344becbc269acd026 to your computer and use it in GitHub Desktop.
Save DaveBatton/86f344becbc269acd026 to your computer and use it in GitHub Desktop.
Saves any images found in the specified view or any of its subviews. Images are saved in the application's Documents directory.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
flightView.hidden = false
stealImagesFromView(view)
}
private func stealImagesFromView(view: UIView) {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths.first!
for subview in view.subviews {
if let imageView = subview as? UIImageView {
if let image = imageView.image {
let filename = "image \(NSDate.timeIntervalSinceReferenceDate()).png"
let path = documentsDirectory + "/" + filename
let imageData = UIImagePNGRepresentation(image)
imageData?.writeToFile(path, atomically: true)
}
}
stealImagesFromView(subview)
}
struct TokenHolder {
static var token: dispatch_once_t = 0;
}
dispatch_once(&TokenHolder.token) { () -> Void in
print("Stolen images can be found here: \(documentsDirectory)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment