OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 AlamoFire 将图像上传到 Uploadcare.com [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:47
标题: ios - 使用 AlamoFire 将图像上传到 Uploadcare.com

我不知道如何使用 Alamofire 将图像上传到 Uploadcare.com (reference) .我应该给出图像或二进制数组的路径?我不确定将字符串转换为 Data 对象是否正确

 func uploadPhoto(imageToUpload : UIImage){

        let keyJson = ("<my_api_key>").data(using: .utf8)!
        let uploadStore = "1".data(using: .utf8)
        let imageData = UIImageJPEGRepresentation(imageToUpload, 0.2)

        Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(keyJson, withName: "UPLOADCARE_PUB_KEY")
                multipartFormData.append(uploadStore!, withName: "UPLOADCARE_STORE")
                multipartFormData.append(imageData!, withName: "file")
            },
            to: "https://upload.uploadcare.com/base/",
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        debugPrint(response)
                    }
                case .failure(let encodingError):
                    debugPrint(encodingError)
                }
            }
        )
    }

结果:

[Response]: <NSHTTPURLResponse: 0x6100002390a0> { URL: https://upload.uploadcare.com/base/ } { status code: 400, headers {
    "Access-Control-Allow-Credentials" = true;
    "Access-Control-Allow-Headers" = "X-PINGOTHER, DNT";
    "Access-Control-Allow-Methods" = "OST, OPTIONS";
    "Access-Control-Allow-Origin" = "*";
    "Access-Control-Max-Age" = 1;
    Connection = "keep-alive";
    "Content-Length" = 31;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Sun, 09 Oct 2016 18:41:45 GMT";
    Server = nginx;
    "X-Frame-Options" = DENY;
} }



Best Answer-推荐答案


正如我所见,使用 Alamofire 有两种方法可以做到这一点。您可以使用像 public func append(_ fileURL: URL, withName name: String) 这样获取文件 URL 的函数,并为它们提供一个文件 URL。或者您可以将图像转换为数据并使用 public func append(_ data: Data, withName name: String)

等函数发送

关于ios - 使用 AlamoFire 将图像上传到 Uploadcare.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947643/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4