使用alamofire在uploadng图像中出错

时间:2018-02-19 18:56:53

标签: ios arrays alamofire

我正在尝试使用Alamofire上传图片。但是当我尝试使用以下消息进行应用程序请求时应用程序崩溃:

  

- [ NSCFNumber dataUsingEncoding:]:无法识别的选择器发送到实例0xb000000000000013   2018-02-20 00:19:58.167574 + 0530 ProSales [63550:5801521] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFNumber dataUsingEncoding:]:无法识别的选择器发送到实例0xb000000000000013'   ***第一次抛出调用堆栈:   (       0 CoreFoundation 0x0000000104ea812b __exceptionPreprocess + 171       1 libobjc.A.dylib 0x0000000103feff41 objc_exception_throw + 48       2 CoreFoundation 0x0000000104f29024 - [NSObject(NSObject)doesNotRecognizeSelector:] + 132       3 CoreFoundation 0x0000000104e2af78 ___ forwarding _ + 1432       4 CoreFoundation 0x0000000104e2a958 _CF_forwarding_prep_0 + 120       5 ProSales 0x0000000103125b03 _T08ProSales17NetworkOperationsC11uploadImageySS3url_s10DictionaryVySSyXlG5paramSo7UIImageC5imageyAA6ResultOc17completionHandlertFZy9Alamofire17MultipartFormDataCcfU_ + 1011       6 ProSales 0x0000000103125de3 _T08ProSales17NetworkOperationsC11uploadImageySS3url_s10DictionaryVySSyXlG5paramSo7UIImageC5imageyAA6ResultOc17completionHandlertFZy9Alamofire17MultipartFormDataCcfU_TA + 99       7 Alamofire 0x0000000103688c74 _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_ + 212       8 Alamofire 0x000000010368c59c _T09Alamofire14SessionManagerC6uploadyyAA17MultipartFormDataCc09multipartfG0_s6UInt64V14usingThresholdAA21URLRequestConvertible_p4withyAC0efG14EncodingResultOcSg18encodingCompletiontFyycfU_TA + 156       9 Alamofire 0x0000000103611269 _T0Ix_IyB_TR + 41       10 libdispatch.dylib 0x0000000109d1b2f7 _dispatch_call_block_and_release + 12       11 libdispatch.dylib 0x0000000109d1c33d _dispatch_client_callout + 8       12 libdispatch.dylib 0x0000000109d283a2 _dispatch_root_queue_drain + 1444       13 libdispatch.dylib 0x0000000109d27da0 _dispatch_worker_thread3 + 132       14 libsystem_pthread.dylib 0x000000010a1e35a2 _pthread_wqthread + 1299       15 libsystem_pthread.dylib 0x000000010a1e307d start_wqthread + 13   )   libc ++ abi.dylib:以NSException类型的未捕获异常终止

上传图片的代码:

class func uploadImage(url:String, param:[String:AnyObject], image:UIImage, completionHandler: @escaping CompletionHandlerType){
    let imageData: Data = UIImagePNGRepresentation(image)!
    Alamofire.upload(multipartFormData:{ multipartFormData in
        multipartFormData.append(imageData, withName: "image",fileName: "image.png", mimeType: "image/png")
        for (key, value) in param {
            multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }
    },
         to:url,
         method:.put,
         headers:getHeader(),
         encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                    completionHandler(.Success(true))
                }
            case .failure(let encodingError):
                print(encodingError)
            }
    })
}

我正在添加如下参数:

    if !name.isEmpty{
        parameters["name"] = name as AnyObject
    }
    if !email.isEmpty{
        parameters["email"] = email as AnyObject
    }
    if !password.isEmpty{
        parameters["password"] = password as AnyObject
    }
    if !mobile.isEmpty{
        parameters["mobile"] = mobile as AnyObject
    }

1 个答案:

答案 0 :(得分:0)

你的问题在这里

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

作为

  param:[String:AnyObject]

包含NSNumber类型的值,因此您可以尝试将NSnumber转换为字符串

   let myString = value.stringValue
   let myData = myString.data(using: .utf8)

修改

  var parameters:[String:String]?

///

  if !name.isEmpty{
        parameters["name"] = name as String
    }
    if !email.isEmpty{
        parameters["email"] = email as String
    }
    if !password.isEmpty{
        parameters["password"] = password as String
    }
    if !mobile.isEmpty{
        parameters["mobile"] = mobile as String
    }


  parameters["userid"] = String(userid)