title: SwiftUI ImagePicker save (Image -> UIImage --> Data) to Core Data
body: <p>Hello everyone I am kind of frustrated, so I hope to get some help. My Project is in SwiftUI. I want to use an image Picker to save the image to Core Data. I achieved to get the ImagePicker to work, but im struggling to convert Image --&gt; UIImage --&gt; Data to then save it. The Project is running without errors, but it doesn't save the image. To know where the prob is I implemented 3 print Statements. The image = gives a value, The UIImage (inputImage) = nil, the Data of course is nil.</p>
  <pre><code>import SwiftUI
  
  
  struct AddNewBean: View {
      
      @Environment(\.presentationMode) var presentationMode
      @Environment(\.managedObjectContext) var moc
      @FetchRequest(entity: Bean.entity(), sortDescriptors: []) var beans: FetchedResults&lt;Bean&gt;
      @State var BeanRoaster: String = &quot;&quot;
      @State var BeanName: String = &quot;&quot;
      @State var BeanStyle: String = &quot;Dark&quot;
      
      @State private var RoastDate = Date()
          var dateFormatter: DateFormatter {
          let formatter = DateFormatter()
          formatter.dateStyle = .long
          return formatter }
      
      @State private var showImagePicker : Bool = false
      @State private var image : Image?
      @State private var inputImage: UIImage?
      @State var imageAlt: Data = .init(count: 0)
      
      let RStyles = [&quot;Dark&quot;, &quot;Medium&quot;, &quot;Light&quot;]
      
      func loadImage() {
          guard let inputImage = inputImage else { return }
          image = Image(uiImage: inputImage)
      }
      
      var body: some View {
          
          NavigationView {
              VStack {
                  Form {
                      VStack {
                          image?.resizable().scaledToFit().aspectRatio(contentMode: .fit)
                          HStack {
                              Spacer()
                              Button(&quot;Open Camera&quot;){
                                  self.showImagePicker = true
                              }.padding(5)
                                  .foregroundColor(Color.white)
                                  .background(Color.accentColor)
                                  .cornerRadius(10)
                              Spacer()
                          }.sheet(isPresented: self.$showImagePicker, onDismiss: loadImage){
                              PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image)
                          }
                      }
                      TextField(&quot;Röster&quot;, text: $BeanRoaster)
                      TextField(&quot;Name der Bohne&quot;, text: $BeanName)
                      Picker(&quot;Roestung&quot;, selection: $BeanStyle) {
                          ForEach(RStyles, id: \.self) {
                              Text($0)
                          }
                      }
                      DatePicker(selection: $RoastDate, in: ...Date(), displayedComponents: .date) {Text(&quot;Röstdatum&quot;)}
     
                      HStack {
                          Spacer()
                          if BeanRoaster != &quot;&quot; &amp;&amp; BeanName != &quot;&quot; {
                              Button(action: {
                                  //....
                                  let pickedImage = self.inputImage?.jpegData(compressionQuality: 1.0)
                                                                  print(&quot;image, inputimage, pickedImage&quot;)
                                  print(self.image as Any) // prints:  Optional(SwiftUI.Image(provider: SwiftUI.ImageProviderBox&lt;__C.UIImage&gt;))
                                  print(self.inputImage as Any) // prints: nil
                                  print(pickedImage as Any) // prints: nil
                                  //.....
                                  let bean = Bean(context: self.moc)
                                  bean.id = UUID()
                                  bean.roaster = &quot;\(self.BeanRoaster)&quot;
                                  bean.name = &quot;\(self.BeanName)&quot;
                                  bean.roastStyle = &quot;\(self.BeanStyle)&quot;
                                  bean.roastDate = self.RoastDate
                                  bean.active = true
                                  bean.img = pickedImage
                                  
                                  try? self.moc.save()
                                  self.presentationMode.wrappedValue.dismiss()
                              }) {
                                  Image(systemName: &quot;tray&quot;)
                                      .foregroundColor(.blue)
                                      .font(.largeTitle)
                                      .padding(.vertical)
                              }
                              Text(&quot;Save&quot;).foregroundColor(.blue)
                              Spacer()
                          } else {
                              HStack {
                                  Spacer()
                                  Text(&quot;Trage Bohnendaten ein!&quot;)
                                  Spacer()
                              }
                          } 
                      } 
  
                      Section {
                          HStack {
                              Spacer()
                              Button(action: {self.presentationMode.wrappedValue.dismiss()}) {
                                  Image(systemName: &quot;&quot;)
                                      .foregroundColor(.red)
                                      .font(.largeTitle)
                                      .padding(.vertical)
                              }
                              Text(&quot;Dismiss&quot;).foregroundColor(.red)
                              Spacer()
                          }
                      }
                  }.navigationBarTitle(&quot;New Bean&quot;)
              }
          }
      }
  }
  </code></pre>