title: Swift - Converting UIImage to jpegData - [Metal] 9072 by 12198 iosurface is too large for GPU
body: <p>I'm trying to convert images picked from the users camera roll into jpegData for me to upload to a server.</p>
  <p>I'm doing the following to covert to Data:</p>
  <p><code>let imageData: Data? = uiImage.jpegData(compressionQuality: 0)</code></p>
  <p>This works fine when converting screenshots. But when I attempt to convert an image taken using the device's camera. It gives me the following error:</p>
  <p><code>[Metal] 9072 by 12198 iosurface is too large for GPU</code></p>
  <p>It still converts it into data, but the data then just contains a blank image. Not the selected image.</p>
  <p>I'm at a loss with where to go with this one, I've tried converting HEIC preloaded onto the device's bundle and it works fine. But when using ones selected from the camera roll, I get the stated error.</p>
  <p>Any help would be appreciated. This is the sample code I'm using to test the issue. (I'm aware it's quite pointless in this form)</p>
  <pre><code>    func convertImage(image: Image?) -&gt; Image? {   //1
          let key = &quot;\(String.random(length: 15))&quot;
  
  
          let uiImage: UIImage = image.asUIImage()
          let imageData: Data? = uiImage.jpegData(compressionQuality: 0)
  
          guard let imageData = imageData else { return nil }
          
          let encodedImage = imageData.base64EncodedString()
          
          let fileManager = FileManager.default
          let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
          let localImageUrl = (documentsPath?.appendingPathComponent(key))
          guard let localImageUrl = localImageUrl else {
              return nil
          }
          try! encodedImage.write(to: localImageUrl, atomically: true, encoding: String.Encoding.utf8)
          
          
          guard let fileContents = try? String(contentsOf: localImageUrl) else {
              return nil
          }
  
          let dataDecoded : Data = Data(base64Encoded: fileContents, options: .ignoreUnknownCharacters)!
          let decodedimage = UIImage(data: dataDecoded)
          
          return (Image(uiImage: decodedimage ?? UIImage(named: &quot;placeholder&quot;)!))
      }
  </code></pre>
  <pre><code>import SwiftUI
  
  struct UploadTesting: View {
      @ObservedObject var crViewModel = ChatroomsViewModel()
      
      @State var showImagePicker = false
      @State var selectedImage: Image? = nil
      
      @State var convertedImage: Image?
      var body: some View {
          VStack {
              Button(&quot;Select&quot;) {
                  showImagePicker.toggle()
              }
              
              selectedImage?.resizable().scaledToFit().frame(width: 300, height: 300)
              
              Button(&quot;Convert&quot;) {
  
  
                  convertedImage = crViewModel.convertImage(image: selectedImage)
              }
              
              convertedImage?.resizable().scaledToFit().frame(width: 200, height: 200).background(Color.red)
          }
          .sheet(isPresented: $showImagePicker) {
              ImagePicker(image: $selectedImage)
          }
  
      }
  }
  
  </code></pre>
  <p>Video of issue: <a href="https://streamable.com/h0uqly" rel="nofollow noreferrer">https://streamable.com/h0uqly</a></p>