1

I have a project where Users select Images from their gallery or snap and it is supposed to be uploaded to a remote server.

Now if I try to upload with a static image that I put in my xcode, The image gets uploaded but the moment I use my ImagePicker and select the Image and pass it to the ImageView, the photo shows but fails to upload to the server. The app does not crash, the image upload just does not go. Any reason why? and any help would be appreciated

NetworkAdapter.instance.uploadImage(status: "user", image: Img.image).subscribe(onNext: { check in
            print("CHECKOUT NOW \(check)")
        }, onError: { error in
            print("CHECKOUT NOW \(error.localizedDescription)")
        }).disposed(by: disposeBag) 

I am using Moya for API call

case .uploadImage(let data):
            let imageData = data.image.jpegData(compressionQuality: 1.0)
            let memberIdData = "\(data.status)".data(using: String.Encoding.utf8) ?? Data()
            var formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: "image", fileName: "user.jpeg", mimeType: "image/jpeg")]
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
King
  • 1,885
  • 3
  • 27
  • 84
  • Your upload request may be getting prematurely disposed. Are you keeping the `disposeBag` around long enough for the request to finish? – dalton_c Jun 29 '19 at 16:53
  • When you say “does not go”, are you saying that you know that the request was never issued or are you saying that you’re not sure if it was sent and rather you just know that it never appeared on the server for some reason? I might want to watch both the working static image scenario and non-working picked image scenario with a tool like [Charles](https://charlesproxy.com) or [WireShark](https://wireshark.org) and see if they differ in any substantive ways. – Rob Jun 29 '19 at 17:08
  • By the way, this process of getting a jpeg with quality of 1.0 means that the image you send to the server is not identical to the asset in the user’s device, and is much larger, too. Once you get this problem behind you, you might want to consider whether you’d rather upload the original asset, or whether you really want to round-trip it through a `UIImage` and JPEG representation. It just depends upon your business need. – Rob Jun 29 '19 at 17:10
  • @Rob what do you suggest – King Jun 29 '19 at 17:12
  • Confirm whether the problem is that the request was not successfully issued or whether the request was sent, but was malformed in some way that prevented the server from accepting it. Like I said, I use tools like [Charles](https://charlesproxy.com) as my “when the rubber hits the road” tool, confirming precisely what was sent down the wire. It takes a little time to configure these tools, but they’re invaluable for diagnosing these sorts of network problems. – Rob Jun 29 '19 at 17:15
  • By the way, are you seeing either of your CHECKOUT NOW messages? – Rob Jun 29 '19 at 17:16
  • yes. I am seeing the CHECKOUT NOW. but since i am not handling error, it is going to onError – King Jun 30 '19 at 20:53

1 Answers1

2

Try this: Change Jpeg data.

case .uploadImage(let data):
           let imageData = data.image.jpegData(compressionQuality: 0)
           let memberIdData = “\(data.status)“.data(using: String.Encoding.utf8) ?? Data()
           var formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: “image”, fileName: “user.jpeg”, mimeType: “image/jpeg”)]
Sajitron
  • 346
  • 3
  • 10