无法通过Trello API将图像作为附件从iOS上传

时间:2016-05-09 03:41:38

标签: ios image mime-types nsdata trello

我尝试使用API​​将图片从iOS应用上传到Trello:

      let imageDataToSend = UIImageJPEGRepresentation(imageToSend, 0.9)

      let parameters: [String: AnyObject] = [
        "file": imageData,
        "mimeType" : "image/jpeg",
        "token" : "mytoken",
        "key" : "mykey",
        "name" : "attachment"

    ]

    oauthswift.client.post("https://trello.com/1/cards/\(card)/attachments", parameters: parameters,
                           success: {
                            data, response in
                            print("attach worked \(self.nsdataToJSON(data))")


        }, failure: { (error) in
            print("post failed\(error)")
    })

附件总是以文本文件形式出现。我尝试过64bitEncoding,UIImagePNGRepresentation。我尝试过设置不同的mimeTypes。

我不明白为什么mimeType总是返回null。这是我收到的数据:

Optional({
bytes = 20681;
date = "2016-05-09T03:33:44.151Z";
edgeColor = "<null>";
id = 57300518f5160afdb6ae5670;
idMember = 54ef9a89772213529008b0a9;
isUpload = 1;
mimeType = "<null>";
name = attachment;
previews =     (
);
url = "https://trello-attachments.s3.amazonaws.com/56b8cf1bca0ff64f7f1ba86a/57300517b871603e15aeb9b5/b8b906a4c952ec3b8d889fcfa37dddfb/Upload";
})

以下是相关API:https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-attachments

2 个答案:

答案 0 :(得分:1)

提交这样的参数时,你应该百分之百地逃避这些值。例如,您可以将字符集定义为仅“未保留”字符(由RFC 3986定义),即字母数字字符加上 - ,。,_和〜: 像这样添加扩展名到你的swift文件:

extension NSCharacterSet {
    class func URLParameterValueCharacterSet() -> NSCharacterSet {
        let characterSet = NSMutableCharacterSet.alphanumericCharacterSet()
        characterSet.addCharactersInString("-._~")

        return characterSet
    }
}

然后你可以做类似的事情:

func percentEscapeString(string: String) -> String {
        return string.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLParameterValueCharacterSet())!
    }

现在发布你的图像:

  var imageData = UIImageJPEGRepresentation(selectedImg, 0.2)
  let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
  let imgStringToPost = percentEscapeString(base64String)
  let parameters: [String: AnyObject] = [
        "file": imgStringToPost,
        "mimeType" : "image/jpeg",
        "token" : "mytoken",
        "key" : "mykey",
        "name" : "attachment"

    ]

答案 1 :(得分:0)

我能够使用不同的库来解决这个问题 - Alamofire和Trello似乎在一起玩得很好。如果有人遇到类似的问题,这就是方法:

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in

            if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.png", mimeType: "image/png")
            }

        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }

        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                print("it worked")
            case .Failure(let encodingError):
                print(encodingError)
            }
    })