Supporting SwiftUI with Core Graphics

Nov 22 2022 · Swift 5.5, iOS 15, Xcode 13

Part 1: Supporting SwiftUI with Core Graphics

02. Create a CGImage

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Anatomy of a View Next episode: 03. Host a UIView in a SwiftUI View

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Before we even touch the code, I’d like to point out a very nice way to debug your view hierarchy. Run your app in the simulator.

ContentView.swift

So we can fix that in ContentView.

GeometryReader {
   ...
}
🟩.ignoresSafeArea()

GridView.swift

So, create a new SwiftUI View file called GridView.swift

func gridImage(size: CGSize) -> UIImage {
  let width = size.width
  let height = size.height
  return UIGraphicsImageRenderer(size: size).image { context in
    
  }
}
UIColor.systemTeal.setStroke()
let path = UIBezierPath()
path.move(to: CGPoint(x: width, y: 0))
path.addLine(to: CGPoint(x: width, y: height))
path.addLine(to: CGPoint(x: 0, y: height))
path.stroke()
Rectangle()
  .ignoresSafeArea()
let size = CGSize(width: 30, height: 30)
let image = Image(uiImage: gridImage(size: size))
...
Rectangle()
  .fill(ImagePaint(image: image))
ZStack {
  Color(uiColor: .tertiarySystemBackground)
  Rectangle()
    .fill(ImagePaint(image: image))
}
.ignoresSafeArea()

BackgroundView.swift

Open up BackgroundView.swift, and change the color in the ZStack to be the GridView

GridView()