Your Second iOS & SwiftUI App

Nov 4 2021 Swift 5.5, iOS 15, Xcode 13

Part 2: Data Flow

12. Image Binding

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 11. Modal Views Next episode: 13. Color Schemes

This video was last updated on Nov 4 2021

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.

At this point, Detail View is set up. When the Photo picker is launched, it will be able to pass a chosen image back through its binding property. But where will it go? Right now it ends in Detail View!

  let book: Book
  @Binding var image: Image?

  var body: some View {
destination: DetailView(book: book, image: $image)
struct ContentView: View {
  var library = Library()

  var body: some View {
    NavigationView {
      List(library.sortedBooks, id: \.self) { book in
@State var library = Library()
      List(library.sortedBooks, id: \.self) { book in
        BookRow(
          book: book,
          image: $library.images[book]
        )
      }
  struct Image: View {
    let image: SwiftUI.Image?
    let title: String
extension Book.Image {
  /// A preview Image.
}
  /// A preview Image.
  init(title: String) {

  }
}
  init(title: String) {
    self.init(
      image: nil,
      title: title
    )
  }
    var body: some View {
      if let image = Image {
      let symbol =
    var body: some View {
      if let image = image {

      } else {
        let symbol =
          SwiftUI.Image(title: title)
            ....
          .foregroundColor(.secondary)
      }
    }
      if let image = image {
        image
          .resizable()
          .scaledToFill()
      } else {
          .scaledToFill()
          .frame(width: size, height: size)
      } else {
      HStack {
        Book.Image(image: image, title: book.title, size: 80)
        
        TitleAndAuthorStack(
      VStack {
        Book.Image(image: image, title: book.title)

        Button("Update Image…") {
    var size: CGFloat?
    let cornerRadius: CGFloat

    var body: some View {
          .frame(width: size, height: size)
          .cornerRadius(cornerRadius)
      } else {
  /// A preview Image.
  init(title: String) {
    self.init(
      uiImage: nil,
      title: title,
      cornerRadius: .init()
    )
        Book.Image(
          uiImage: image,
          title: book.title,
          cornerRadius: 16
        )
          cornerRadius: 16
        )
        .scaledToFit()
          size: 80,
          cornerRadius: 12
        )