繁体   English   中英

如何使用Alamofire SwiftyJSON将图像上传到JSON Swift 3

[英]How to upload an image using Alamofire SwiftyJSON to a JSON swift 3

我正在尝试使用alamofireSwiftyJSON将图像发送到我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM