Your Second iOS & SwiftUI App

Nov 4 2021 · Swift 5.5, iOS 15, Xcode 13

Part 2: Data Flow

12. Image Binding

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: 11. Modal Views Next episode: 13. Color Schemes

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.

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
        )