2017-10-15 65 views
0

我可以發送圖像的API(節制),像這樣它在郵遞員鏈接到一個「媒體」 PARAM:如何使用Alamofire上傳圖像和引用的文件中PARAMS

enter image description here

現在我正在試圖迅速做同樣的事情。我遵循示例here但出現錯誤:未指定媒體。繼承人我的代碼:附加的圖像文件沒有正確鏈接到媒體參數?

let image = self.descriptionImage.image! 
    let parameters = [ 
     "api_user": "xxxxx", 
     "api_secret": "xxxxx", 
     "models": "nudity,wad", 
     "media": "file.png" 
    ] 

    Alamofire.upload(multipartFormData: { multipartFormData in 
     if let imageData = UIImageJPEGRepresentation(image, 1) { 
      multipartFormData.append(imageData, withName: "file.png", fileName: "file.png", mimeType: "image/png") 
     } 

     for p in parameters { 
      let value = p.value 
      multipartFormData.append((value.data(using: .utf8))!, withName: p.key) 
     }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil, 
      encodingCompletion: { encodingResult in 
       switch encodingResult { 
       case .success(let upload, _, _): 
        upload.response { [weak self] response in 
         guard let strongSelf = self else { 
          return 
         } 
         print(response.data) 
         print("strongSelf") 

         debugPrint(response) 

        } 
       case .failure(let encodingError): 
        print("error:\(encodingError)") 
       } 
    }) 

回答

1

對圖像的參數應該在多形式的數據來指定,嘗試更改代碼下面:

let image = self.descriptionImage.image! 
let parameters = [ 
    "api_user": "xxxxx", 
    "api_secret": "xxxxx", 
    "models": "nudity,wad" 
] 

Alamofire.upload(multipartFormData: { multipartFormData in 
    if let imageData = UIImagePNGRepresentation(image) { 
     multipartFormData.append(imageData, withName: "media", fileName: "file.png", mimeType: "image/png") 
    } 

    for p in parameters { 
     let value = p.value 
     multipartFormData.append((value.data(using: .utf8))!, withName: p.key) 
    }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil, 
     encodingCompletion: { encodingResult in 
      switch encodingResult { 
      case .success(let upload, _, _): 
       upload.response { [weak self] response in 
        guard let strongSelf = self else { 
         return 
        } 
        print(response.data) 
        print("strongSelf") 

        debugPrint(response) 

       } 
      case .failure(let encodingError): 
       print("error:\(encodingError)") 
      } 
}) 
+0

OMG感謝你這麼多,我已經與徹夜狂歡一直在掙扎:DDD – user3517546

+0

@ user3517546很高興它可以幫助你。 – chengsam

相關問題