Created
January 15, 2018 00:17
-
-
Save robinkunde/aa69cc46e7d3768afd0ce2c691df807b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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