首页 文章

使用alamofire和swifty json将图像上传到Web服务会返回错误失败

提问于
浏览
0

这是我的代码:

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背景[2690:63834] [BoringSSL]函数boringssl_session_errorlog:第2868行[boringssl_session_write] SSL_ERROR_SYSCALL(5):操作在外部失败库{“消息”:“处理请求时出错 . ”,“StackTrace”:“”,“ExceptionType”:“”}

1 回答

  • 0

    如果要将图像传递给字符串参数,则必须将图像转换为 base64EncodedString

    let imageData:NSData = UIImagePNGRepresentation(self.Image)!
    let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
    

    并在您的情况下将 strBase64 传递给 Base64Image 键的参数值 .

相关问题