这是我的代码:
import UIKit
import Alamofire
import SwiftyJSON
import Alamofire_SwiftyJSON
class ViewController: UIViewController , UINavigationControllerDelegate , UIImagePickerControllerDelegate{
var Image = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ChosePhoto(_ sender: Any) {
let getimage = UIImagePickerController()
getimage.delegate = self
getimage.sourceType = UIImagePickerControllerSourceType.photoLibrary
getimage.allowsEditing = false
self.present(getimage, animated: true, completion: nil)
}
@IBAction func Uploading(_ sender: Any) {
let requestURL = UU
let param = [
"S1" : "7f3a92a2-9c25-4b96-ba20-489c6412e36f",
"S2" : "F/EogECr4Z1GwtVQANHq",
"S3" : "EE0009BB-CF91-4ECB-AB5C-3F9BA6CA0A61" ,
"Base64Image" : self.Image ,
"FileId" : "bd535daf-21c6-4b4b-98d5-7042f7789aab",
"Extension" : "jpeg" ,
"CommentId" : "c1f57f35-1c81-4b4e-a1b5-18e14eb761e7"
]
let header = [
"Content-Type" : "application/json; charset=utf-8"
]
Alamofire.request(requestURL, method: HTTPMethod.post, parameters: param, encoding: JSONEncoding.default, headers: header).responseString { response in
switch response.result {
case .success:
let mess = response.result.value!
print(mess)
case .failure(let error):
print(error)
}
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage?
let data : Data = UIImageJPEGRepresentation(image!, 0.2)!
let Ima = data.base64EncodedString(options: .endLineWithCarriageReturn)
self.Image = Ima
print(self.Image)
self.dismiss(animated: true, completion: nil)
}
}
这是错误:
SSL_ERROR_SYSCALL(5):库外部失败的操作 2018-08-31 18:04:56.890342-0700 background [2690:63834] [BoringSSL]函数boringssl_session_errorlog:第2868行[boringssl_session_write] SSL_ERROR_SYSCALL(5):操作在库外部失败 {“ Message”:“处理请求时出错。”,“ StackTrace”:“”,“ ExceptionType”:“”}
答案 0 :(得分:0)
如果要将图像传递给字符串参数,则必须将图像转换为base64EncodedString
let imageData:NSData = UIImagePNGRepresentation(self.Image)!
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
然后根据情况将strBase64
传递给Base64Image
键的参数值。