Created
October 2, 2024 20:47
-
-
Save gerdemb/0ab86dad299a779aa9cd7e1dcd0d4450 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
| // | |
| // AppIconView.swift | |
| // This view represents a customizable app icon using SwiftUI. | |
| // | |
| // To replace `AppIconView` with your own design: | |
| // - Modify the contents of the `body` to implement your desired app icon layout. | |
| // - You can use any combination of shapes, colors, images, and text. | |
| // - Preview the icon using `AppIconPreview` and save it when you're satisfied. | |
| // | |
| import SwiftUI | |
| struct AppIconView: View { | |
| var body: some View { | |
| ZStack { | |
| Color.white | |
| .edgesIgnoringSafeArea(.all) | |
| Circle() | |
| .frame(width: 750, height: 750) | |
| .shadow(radius: 10) | |
| Image(systemName: "shield.lefthalf.fill") | |
| .resizable() | |
| .foregroundColor(.white) | |
| .frame(width: 300, height: 300) | |
| .shadow(radius: 5) | |
| } | |
| } | |
| } | |
| struct AppIconPreview<Content: View>: View { | |
| let appIconView: Content | |
| let appIconPath: String | |
| var body: some View { | |
| appIconView | |
| .frame(width: 1024, height: 1024) // Render at actual AppIcon size | |
| .overlay( // Add rounded corners | |
| RoundedRectangle(cornerRadius: 180) | |
| .stroke(Color.gray, lineWidth: 1) | |
| ) | |
| .scaleEffect(0.25) // Scale back down to fit in Preview | |
| .frame(width: 256, height: 256) // Crop frame back to new size | |
| .padding(.bottom) | |
| Button("Save") { | |
| render() | |
| } | |
| } | |
| @MainActor func render() { | |
| let fileURL = URL(fileURLWithPath: appIconPath) | |
| let imageRenderer = ImageRenderer(content: appIconView.frame(width: 1024, height: 1024)) | |
| if let uiImage = imageRenderer.uiImage, | |
| let pngData = uiImage.pngData() { | |
| do { | |
| try pngData.write(to: fileURL) | |
| print("Image saved to \(fileURL)") | |
| } catch { | |
| print("Error saving image: \(error)") | |
| } | |
| } else { | |
| print("Rendering error") | |
| } | |
| } | |
| } | |
| #Preview { | |
| AppIconPreview( | |
| appIconView: AppIconView(), | |
| appIconPath: "/icon.png" | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment