Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Alamofire上载图像未成功_Ios_Swift_Alamofire - Fatal编程技术网

Ios 使用Alamofire上载图像未成功

Ios 使用Alamofire上载图像未成功,ios,swift,alamofire,Ios,Swift,Alamofire,我正在尝试使用主体参数img(图像)和id(可以是任何数字)向端点发出POST请求 这适用于Postman(除非id丢失,否则服务器不会以失败/成功响应): 但是,在iOS应用程序端: class func postPreviewImage(operaID: Int, image: UIImage, completion: @escaping () -> Void) { let URL_CORDINATE = "http://artaugmentedreality.com/api

我正在尝试使用主体参数img(图像)和id(可以是任何数字)向端点发出POST请求

这适用于Postman(除非
id
丢失,否则服务器不会以失败/成功响应):

但是,在iOS应用程序端:

class func postPreviewImage(operaID: Int, image: UIImage, completion: @escaping () -> Void) {
    let URL_CORDINATE = "http://artaugmentedreality.com/api/postpreview/"
    print("Trying to post a preview of opera with ID:", operaID)

    guard let imgData = image.jpegData(compressionQuality: 0.2) else {
        print("Error while retrieving image data")
        return
    }

    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "img", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
        multipartFormData.append(operaID.data, withName: "id")
    },
                     to: URL_CORDINATE,
                     method: .post)
    { (result) in
        print("Result of Alamofire call is", result)

        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })
        case .failure(let encodingError):
            print("Error while uploading image:", encodingError)
        }
    }
}

extension Int {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout<Int>.size)
    }
}
Alamofire中的错误代码,可能是因为域不是HTTP。但是,我将该域包括在info.plist中允许的域列表中:


因此,我不知道如何从这里开始。

您正在随图像发送ID参数。所以尝试使用utf8编码

 multipartFormData.append(operaID.data(using: .utf8)!, withName: "id")

嗨,塞萨尔!我已经给出了答案,谢谢!我看到您正在以[String:String]的形式传递所有参数,但是,我的id是一个整数。你认为我在主体参数中传递的是整数数据而不是字符串数据有关系吗?我完全不知道,但我认为这可能会导致问题……在plist文件中看不到这个键“允许任意加载”。@ShauketSheikh现在添加了,但没有改变任何东西。operaID是一个整数,所以
数据(使用:)
不可用。我应该先将id转换为字符串吗?是的,也可以尝试:)
 multipartFormData.append(operaID.data(using: .utf8)!, withName: "id")