1

I am implementing User Profile Module. For that I want to store the user profile, which user selected from camera or gallery. I want to pass it into the API, but I don't know how to do it.

Below is my code..

var request = URLRequest(url: URL(string: "http://xxxx.xx/UpdateEmp_test?xx=\(xx)&PermanentAddress=\(ADDrEss)&PermVillage=\(townCITY)&PermTaluk=\(taulkcode!)&PermDist=\(districtcode!)&PermPinCode=\(PINcode)&PermState=\(statecode!)&FatherName=\(FatherNaME)&MotherName=\(MothERName)&profile_image=\(imagePath)")!)
        print(request)

        request.httpMethod = "POST"
        let session = URLSession(configuration: .default)
        session.dataTask(with: request) {data, response, error in
            guard error == nil && data != nil else
            {
                let alert = UIAlertController(title: "Check your Internet Connection", message: "", preferredStyle: .alert)

                self.present(alert, animated: true, completion: nil)

                let when = DispatchTime.now() + 3
                DispatchQueue.main.asyncAfter(deadline: when)
                {
                    // your code with delay
                    alert.dismiss(animated: true, completion: nil)
                }
                return
            }


            do
            {
                let json = try JSONSerialization.jsonObject(with: data!, options: [])
                print(json)
                let message  = (json as AnyObject).value(forKey: "message") as! NSString
                print(message)

                DispatchQueue.main.sync {
                    if message == "success" {
                        let alert = CustomAlert(title:"Request Updated Successfully!")
                        alert.show(animated: true)

                        self.dismiss(animated: true, completion:nil)
                    }
                    else if message == "Already"{
                        let alert = CustomAlert(title:"Already Request Updated")
                        alert.show(animated: true)
                    }
                    else {
                        let alert = CustomAlert(title:"Failed,Try Again!")
                        alert.show(animated: true)
                    }
                }
            }
            catch
            {

            }
            }.resume()
zero323
  • 322,348
  • 103
  • 959
  • 935
Eniya Sri
  • 33
  • 1
  • 5
  • 1
    don't use `Base64` `string` to u upload image to server. use `multipart` to upload image to server. – Kuldeep Apr 28 '18 at 08:34

1 Answers1

0

You can convert the image to Base64 string and sent it to server:

let imageData:NSData = UIImagePNGRepresentation(yourUIImage.image)!

Encoding:

let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)

Decoding:

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
yourImageView.image = decodedimage
Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • I am passing strBase64 values to Api getting thread error what to do?? is am I passing strbase64 data is correct ?? or some thing else – Eniya Sri Apr 28 '18 at 08:36
  • what is the error ? – Arash Etemad Apr 28 '18 at 08:56
  • 1
    getting red line in var request = URLRequest(url: URL(string: "http://xxxx.xx/UpdateEmp_test?xx=\(xx)&PermanentAddress=\(ADDrEss)&PermVillage=\(townCITY)&PermTaluk=\(taulkcode!)&PermDist=\(districtcode!)&PermPinCode=\(PINcode)&PermState=\(statecode!)&FatherName=\(FatherNaME)&MotherName=\(MothERName)&profile_image=\(imagePath)")!) this line ... – Eniya Sri Apr 28 '18 at 09:02