1

I have a parameter like "currentPhoto" and link API key as http://someapikey.

A user has to choose a photo from photo library and upload it to this link. I need to send this photo as a parameter.

Example:
The user chose a photo to let's name it as myPhoto. I need to send this photo to link http://someapikey as parameter "currentPhoto": myPhoto.

It should post request.

niton
  • 8,771
  • 21
  • 32
  • 52
Jack
  • 291
  • 2
  • 6
  • 12

1 Answers1

1

You can try this code...

 var strBase64: NSString!
 let image = UIImage(named: "images.jpeg");
 let imageData = UIImagePNGRepresentation(image!)! as NSData
 strBase64 = imageData.base64EncodedString(options: .lineLength64Characters) as NSString

 let url: String = "http://someapikey"
 let parameter = ["currentPhoto": strBase64] as [String : Any]

 Alamofire.request(url, method: .post, parameters: parameter, encoding: JSONEncoding.default)
            .responseJSON { response in
                debugPrint(response)
  }
Samiul Islam Sami
  • 791
  • 13
  • 24
  • Did not help) I didn't know how to send request, and I have send it as usual in this format: Alamofire.request(url, method: .post, parameters: postString, encoding: JSONEncoding(options: [])).responseJSON { response in } // This gave me a fatal error – Jack Jun 18 '17 at 06:33
  • I think there is a problem in your api key. Is our api key is valid? – Samiul Islam Sami Jun 18 '17 at 07:50
  • yes, I checked it in postman. Everything is fine. But, in code I can not send an image. – Jack Jun 18 '17 at 08:05
  • https://stackoverflow.com/questions/31949118/send-post-parameters-with-multipartformdata-using-alamofire-in-ios-swift Here is a perfect example, but I need it in swift 3 – Jack Jun 18 '17 at 08:08