Swift 3.1如何将图像作为base64插入到HTTPS请求的主体中

问题描述:

我想使用HTTPS POST请求将图像发送到服务器。我需要将图像作为base64数据发送。我已经将图像数据转换为base64编码的字符串,但我无法将其转换为Data以将其放入httpBody中。Swift 3.1如何将图像作为base64插入到HTTPS请求的主体中

//encode image to base64 
    let chosenImage = imageView.image 
    let imageData:NSData = UIImagePNGRepresentation(chosenImage!)! as NSData 
    let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters) 
    let dataBase64: Data = Data(strBase64) 


    let uploadUrlString = "https://api.imgur.com/3/upload" 
    let uploadUrl = NSURL(string: uploadUrlString) 
    let uploadRequest = NSMutableURLRequest(url: uploadUrl! as URL) 
    uploadRequest.addValue("Client-ID f7e5521d5dd4245", forHTTPHeaderField: "Authorization") 
    uploadRequest.httpMethod = "POST" 
    uploadRequest.httpBody = imageData 
    let uploadSession = URLSession.shared 
    let postRequest = uploadSession.dataTask(with: uploadRequest as URLRequest) { (data, response, error) -> Void in 
     if let antwort = response as? HTTPURLResponse { 
      let code = antwort.statusCode 
      print(code) 
     } 
    } 
    postRequest.resume() 

看到StakOverflow的一些解决方案,但没有得到如何正确地做到这一点。 请指教。

+0

您需要使用JSONSerialization.data(withJSONObject:JSONObject的)到您的JSON对象转换为数据 –

+0

您的JSON顶层的对象应该是一个字典或数组 –

+0

在斯威夫特3,你应该使用NSMutableURLRequest的的URLRequest。你只需要声明它为var。使用JPEG代替PNG会更好。请注意,PNG不保存图像方向 –

最后,我根据Leo Dabus的建议改了一下代码。而且我保证它无需任何转换工作正常为base64:

let chosenImage = imageView.image 
    let imageData = UIImageJPEGRepresentation(chosenImage!, 1) 
    let uploadUrlString = "https://api.imgur.com/3/upload" 
    let uploadUrl = URL(string: uploadUrlString) 

    var postRequest = URLRequest.init(url: uploadUrl!) 
    postRequest.addValue("Client-ID XXXXXX", forHTTPHeaderField: "Authorization") 
    postRequest.httpMethod = "POST" 
    postRequest.httpBody = imageData 

    let uploadSession = URLSession.shared 
    let executePostRequest = uploadSession.dataTask(with: postRequest as URLRequest) { (data, response, error) -> Void in 
     if let response = response as? HTTPURLResponse 
     { 
      print(response.statusCode) 
     } 
     if let data = data 
     { 
      let json = String(data: data, encoding: String.Encoding.utf8) 
      print("Response data: \(String(describing: json))") 
     } 
    } 
    executePostRequest.resume() 

所以上传图片的时候,我得到的结果在JSON的身体,它显示我的链接上传的图片:

"link\":\"http:\\/\\/i.imgur.com\\/ePOUIyN.jpg\"},\"success\":true,\"status\":200}") 
+0

'postRequest as URLRequest'没有任何意义,从URLRequest转换为URLRequest。将其更改为'uploadSession.dataTask(with:postRequest){数据,响应错误' –

+0

并添加'guard let data = data,error == nil else {return}' –

+0

并且更短'let json = String(data:数据,编码:.utf8)' –

我有同样的问题...这里是我的解决方案插入BASE64字符串到电子邮件正文: // SWIFT 3.X

//convert to Data 


let image = UIImage(data: yourUIImage! as Data) 


//convert to base64 String 
let imageData: NSData = UIImagePNGRepresentation(image!)! as NSData 


let imageStr = imageData.base64EncodedString(options:.endLineWithCarriageReturn) 


    // insert this into your HTML code 
let tmpHTMLimage = "<img class=\"no-show\" src=\"data:image/png;base64," + imageStr + "\" alt=\"Image\" height=\"80\" width=\"80\"/>" 
  • 解决方案:使用.endLineWithCarriage返回而不是 .lineLength64Characters !!!

    我真的希望这会帮助你们中的一些人!