使用Alamofire上载图片的问题(有图片作为参数)

编程入门 行业动态 更新时间:2024-10-24 16:28:04
本文介绍了使用Alamofire上载图片的问题(有图片作为参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这就是我尝试使用Alamofire上传图像的方式。但是程序崩溃,提示类似... 'NSInvalidArgumentException',原因:'-[____ SwiftTypePreservingNSNumber dataUsingEncoding:]:无法识别的选择器已发送至实例... 我无法找出确切原因。这就是我发出请求的方式...

This is how I am trying to upload an image using Alamofire. But the program crashes saying something like...'NSInvalidArgumentException', reason: '-[_SwiftTypePreservingNSNumber dataUsingEncoding:]: unrecognized selector sent to instance... I'm not able figure the exact reason.This is how I'm making the request...

for i in 1...(imageArray.count) { for img in imageArray { let url = "myapp/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 }}}}

希望有人可以帮忙...谢谢...:)

Hope somebody can help...Thanks...:)

推荐答案

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

Try to use this code.It's working for me.

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 }

更多推荐

使用Alamofire上载图片的问题(有图片作为参数)

本文发布于:2023-11-26 08:13:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片   参数   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!