Skip to content

Instantly share code, notes, and snippets.

@robinkunde
Created January 15, 2018 00:17
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 robinkunde/aa69cc46e7d3768afd0ce2c691df807b to your computer and use it in GitHub Desktop.
Save robinkunde/aa69cc46e7d3768afd0ce2c691df807b to your computer and use it in GitHub Desktop.
func exportVisibleImages() {
for (windowIndex, window) in UIApplication.shared.windows.enumerated() {
let prefix = "iOS\(ProcessInfo.processInfo.operatingSystemVersion.majorVersion)_window\(windowIndex)_images"
exportImages(fromRootView: window, toDirectory: "/path/to/target/directory", prefix: prefix)
}
}
func exportImages(fromRootView rootView: UIView, toDirectory directory: String, prefix: String) {
let directoryURL = URL(fileURLWithPath: directory, isDirectory: true)
try! FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
let imageViews = gatherImageViews(below: rootView)
for (index, imageView) in imageViews.enumerated() {
guard let image = imageView.image else { continue }
let url = directoryURL.appendingPathComponent("\(prefix)_\(index)@\(Int(image.scale))x.png", isDirectory: false)
try! UIImagePNGRepresentation(image)!.write(to: url)
}
}
func gatherImageViews(below view: UIView) -> [UIImageView] {
var imageViews: [UIImageView] = []
for subview in view.subviews {
if let imageView = subview as? UIImageView {
imageViews.append(imageView)
}
imageViews += gatherImageViews(below: subview)
}
return imageViews
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment