ios - 使用 Alamofire 上传图像的问题(有图像作为参数)

标签 ios swift alamofire

这就是我尝试使用 Alamofire 上传图像的方式。但是程序崩溃了,说...'NSInvalidArgumentException',reason:'-[_SwiftTypePreservingNSNumber dataUsingEncoding:]:无法识别的选择器发送到实例...我无法弄清楚确切的原因。这这就是我提出请求的方式...

    for i in 1...(imageArray.count) {
        for img in imageArray {

            let url = "http://myapp.com/a/images_upload"
            let headers = [ "Content-Type":"application/x-www-form-urlencoded"]

  let imageData: Data = (UIImageJPEGRepresentation(img, 0.6) as Data?)!
            print(imageData)

            let parameters: [String: Any]  = [
                    "access_token":  commonVarForAccessToken,
                    "seller_id": idForNewOldUser, 
                    "product_id": self.productId,
                    "is_default": "1",
                    "sequence": i,
                    "image": imageData  ]

            Alamofire.upload(multipartFormData: { (multipartFormData) in
                print(parameters)

                multipartFormData.append(imageData as Data, withName: "home-\(self.index)", fileName: "home-\(self.index)", mimeType: "image/jpeg")

                for (key, value) in parameters {
                    print(key,value)

                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)

                }
            }, to:url)
            { (result) in
                switch result {
                case .success(let upload, _, _):
                    upload.uploadProgress(closure: { (Progress) in
                        //Print progress
                    })
                    upload.responseJSON { response in

                        print(response.request)  // original URL request

                         if let JSON = response.result.value {
                            print("JSON: \(JSON)")
                        }

                    }
                case .failure(let encodingError):

                    print(encodingError)
                    break
                }}}}

希望有人能帮忙...谢谢...:)

最佳答案

尝试使用此代码。它对我有用。

let para: [String: Any]

Alamofire.upload(multipartFormData: {(multipartFormData) in

for i in 0..<uploadImages.count{         multipartFormData.append(UIImageJPEGRepresentation(uploadImages[i], 0.3)!, withName: "image\(i)", fileName: "swift_file\(i).jpeg", mimeType: "image/jpg")             
        }

        for (key, value ) in para {
            multipartFormData.append((value).data(using: String.Encoding.utf8)!, withName: key)
        }
    }, to: apiURL)
    { (result) in
        switch result {
        case .success(let upload, _,_ ):

            upload.uploadProgress(closure: { (progress) in
                UILabel().text = "\((progress.fractionCompleted * 100)) %"
                print (progress.fractionCompleted * 100)
            })


            upload.responseJSON { response in

                guard ((response.result.value) != nil) else{
            print(response.result.error!.localizedDescription)
                    return
                }

关于ios - 使用 Alamofire 上传图像的问题(有图像作为参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46661121/

相关文章:

ios - 在 sharekit 中仅启用电子邮件和 Facebook

ios - Core Data 中的并发获取请求

Swift 将逗号分隔的字符串转换为 NSMutableArray

ios - 无法在 Xcode 6 Beta 5 上运行测试

ios - 将区号与电话号码分开 - iOS

ios - 如何使用带有外部依赖的框架

xml - SWXMLHash 迭代错误

swift - 使用 Alamofire 将 Curl 命令转换为 swift

ios - 停止永远重复的 SKAction - Sprite Kit

ios - 如何检测 "clear"通知