[英]How to upload an image using Alamofire SwiftyJSON to a JSON swift 3
我正在尝试使用alamofire
和SwiftyJSON
将图像发送到我的API。 我一直在尝试一些不同的方法来做到这一点,但是到目前为止还没有成功。 我尝试的最后一个是base64EncodedString
,但无法正常工作。 我将变量styleStrength作为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)
}
作为回报,我有一个非常大的图像名称,但API不会更新。
尝试使用UIImageJPEGRepresentation代替UIImagePNGRepresentation。
在代码中替换这些行
let imageData = UIImagePNGRepresentation(image)
let base64String = imageData!.base64EncodedString(options: [])
styleStrengthData["Personal_Style"]["profile_pic_file"].stringValue = base64String + ".jpg"
与
let imageData = UIImageJPEGRepresentation(image,0.5)! as NSData
let strBase64ProfilePic = imageData.base64EncodedString()
styleStrengthData["Personal_Style"]["profile_pic_file"].stringValue = base64String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.