在iOS swift中将图像上传到服务器
Posted
技术标签:
【中文标题】在iOS swift中将图像上传到服务器【英文标题】:upload image to server in iOS swift 【发布时间】:2020-06-12 17:57:04 【问题描述】:我是 swift 新手。
我不知道如何使用 Alamofire 将图像从 UIImage Piker 控制器上传到服务器。不幸的是,我从上周开始尝试了许多来自 *** 和 Google 等的解决方案。但我想不通。
请任何人帮助我。
现在我尝试实现这段代码:
func uploadImgRiderAPI()
print("I am in uploadImgAPI")
let imgData = UIImageJPEGRepresentation(profilePicOut.image!, 0.2)! //Error: UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'
let parameters : [String : Any] = ["image": imgData, "riderId" : "5ed4eecfe3ec0c6b7c8e4990"]
guard let url = URL(string: "\(Constents.baseURL)/rider/uploadImage") else
print("Invalid URL")
return
AF.upload(multipartFormData: multipartFormData in
multipartFormData.append(imgData, withName: "image",fileName: "alkaram.png", mimeType: "image/jpg")
for (key, value) in parameters
multipartFormData.append(value.data(using: String.Encoding.utf8), withName: key) // Error: Cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members
, to:url) (result) in
switch result
case .success(let upload, _, _): //Error : Pattern cannot match values of type 'URLRequest'
upload.uploadProgress(closure: (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
)
upload.responseJSON response in
print(response.result.value)
if let result = response.data
do
let jsonData = try JSONEncoder().encode(parameters)
let jsonString = String(data: jsonData, encoding: .utf8)
let url = URL(string: self.retrivedRiderProfileImg!)
print("Complete URL is :-> \(url)")
let placeholderImage = UIImage(named: "referral_icon")!
self.profilePicOut.af_setImage(withURL: url!, placeholderImage: placeholderImage)
print("JSON String : " + jsonString!)
print("And Responce is : \(response)")
catch
print(error)
case .failure(let encodingError): //Error : Pattern cannot match values of type 'URLRequest'
print(encodingError)
【问题讨论】:
那么您面临的问题是什么? case .success(let upload, _, _): //Error : Pattern cannot match values of type 'URLRequest' 让 imgData = UIImageJPEGRepresentation(profilePicOut.image!, 0.2)! //错误:UIImageJPEGRepresentation'已被实例方法'UIImage.jpegData(compressionQuality:)'替换 case .failure(let encodingError): //Error : Pattern cannot match values of type 'URLRequest' 【参考方案1】:首先,您需要将 UIImageJPEGRepresentation
的旧 API 替换为新的 jpegData(compressionQuality:)
方法。
替换这个:
let imgData = UIImageJPEGRepresentation(profilePicOut.image!, 0.2)!
有了这个:
guard let imgData = profilePicOut.image?.jpegData(compressionQuality: 0.2) else return
并替换case
,其中仅返回一个 URLRequest 参数,以仅接受一个参数作为错误状态。
替换这个:
case .success(let upload, _, _):
有了这个:
case .success(let upload):
【讨论】:
case .success(let upload): //Pattern不能匹配'URLRequest'类型的值 multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8), withName: key) //不能使用类型为'的参数列表调用'data'(使用: String.Encoding)'以上是关于在iOS swift中将图像上传到服务器的主要内容,如果未能解决你的问题,请参考以下文章
使用 Swift 3 在 iOS 10 中将图像从服务器加载到 Apple Map 中的注释视图
在 Swift 2.0 中从 iOS 应用程序上传图像到服务器