Last active
November 8, 2023 10:55
-
-
Save ku/3e965b5d99c02e03d45dfce0acdc8731 to your computer and use it in GitHub Desktop.
dump SF symbols as PNG
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
import UIKit | |
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
var image = UIImage(systemName: "plus.circle.fill")! | |
image = image.withTintColor(.init( | |
red: 0x37/255.0, | |
green: 0xc6/255.0, | |
blue: 0x59 / 255.0, | |
alpha: 1.0), renderingMode: .alwaysOriginal) | |
let updatedConfiguration = UIImage.SymbolConfiguration(weight: .bold) | |
let sizes: [CGFloat] = [120, 152, 167, 180, 1024] | |
sizes.forEach { x in | |
let v = UIImageView(image: image) | |
v.preferredSymbolConfiguration = updatedConfiguration | |
v.frame = CGRect(x: 0, y: 0, width: x, height: x) | |
v.contentMode = .scaleAspectFit | |
v.tintColor = .black | |
let scale = 1 | |
UIGraphicsBeginImageContextWithOptions(v.layer.frame.size, false, CGFloat(scale)) | |
v.layer.render(in: UIGraphicsGetCurrentContext()!) | |
let viewImage = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
let data = viewImage.pngData() | |
let documentsDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as! String | |
let n = Int(x) | |
let writePath = URL(fileURLWithPath: documentsDir + ("/\(n)-\(n).png")) | |
print(writePath) | |
try! data?.write(to: writePath) | |
view.addSubview(v) | |
self.view = view | |
} | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this!