【问题标题】:How to send a photo as parameter如何将照片作为参数发送
【发布时间】:2017-11-20 09:10:43
【问题描述】:

我有一个类似“currentPhoto”的参数,并将 API 密钥链接为 http://someapikey

用户必须从照片库中选择一张照片并将其上传到此链接。我需要将这张照片作为参数发送。

示例:
用户选择了一张照片,我们将其命名为 myPhoto。我需要将此照片发送到链接http://someapikey 作为参数“currentPhoto”:myPhoto。

它应该发布请求。

【问题讨论】:

    标签: json post swift3 alamofire photo


    【解决方案1】:

    你可以试试这个代码...

     var strBase64: NSString!
     let image = UIImage(named: "images.jpeg");
     let imageData = UIImagePNGRepresentation(image!)! as NSData
     strBase64 = imageData.base64EncodedString(options: .lineLength64Characters) as NSString
    
     let url: String = "http://someapikey"
     let parameter = ["currentPhoto": strBase64] as [String : Any]
    
     Alamofire.request(url, method: .post, parameters: parameter, encoding: JSONEncoding.default)
                .responseJSON { response in
                    debugPrint(response)
      }
    

    【讨论】:

    • 没有帮助)我不知道如何发送请求,我像往常一样以这种格式发送它:Alamofire.request(url, method: .post, parameters: postString, encoding: JSONEncoding(options: [])).responseJSON { response in } // 这给了我一个致命错误
    • 我认为您的 api 密钥有问题。我们的 api 密钥有效吗?
    • 是的,我在邮递员那里查过了。一切顺利。但是,在代码中我无法发送图像。
    • stackoverflow.com/questions/31949118/… 这是一个完美的例子,但我在 swift 3 中需要它
    猜你喜欢
    • 2015-09-26
    • 2020-11-28
    • 1970-01-01
    • 2021-07-07
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多