Supporting SwiftUI with Core Graphics

Nov 22 2022 Swift 5.5, iOS 15, Xcode 13

Part 1: Supporting SwiftUI with Core Graphics

2. Create a CGImage

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 1. Anatomy of a View Next episode: 3. Host a UIView in a SwiftUI View

This video Create a CGImage was last updated on Nov 22 2022

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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()