I'm trying to send an image to my API using alamofire
and SwiftyJSON
. I've been trying some different ways to do that, however no success so far. The last one I tried is the base64EncodedString
and is not working. I have the variable styleStrength as a JSON.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImage.image = image
self.profileImage.isHidden = false
self.profileImage.contentMode = UIViewContentMode.scaleAspectFill
let imageData = UIImagePNGRepresentation(image)
let base64String = imageData!.base64EncodedString(options: [])
var styleStrengthData = JobSeekerBuildProfile.loadJobSeekerBuildProfile()
styleStrengthData["Personal_Style"]["profile_pic_file"].stringValue = base64String + ".jpg"
JobSeekerBuildProfile.storeJobSeekerBuildProfile(j: styleStrengthData)
}else{
//error message
}
self.dismiss(animated: true, completion: nil)
}
As return I have a really huge image name but the API doesn't update.
Try to use the UIImageJPEGRepresentation instead of UIImagePNGRepresentation.
Replace these lines in your code
let imageData = UIImagePNGRepresentation(image)
let base64String = imageData!.base64EncodedString(options: [])
styleStrengthData["Personal_Style"]["profile_pic_file"].stringValue = base64String + ".jpg"
with the
let imageData = UIImageJPEGRepresentation(image,0.5)! as NSData
let strBase64ProfilePic = imageData.base64EncodedString()
styleStrengthData["Personal_Style"]["profile_pic_file"].stringValue = base64String
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.