将图片快速上传到iOS中的服务器

问题描述 投票:0回答:1

我是新手。

我不知道如何使用Alamofire将图像从UIImage Piker Controller上传到服务器。不幸的是,上周我尝试了stackOverflow和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)
            }
        }
    }


ios swift alamofire iosdeployment
1个回答
0
投票

首先,您需要用新的UIImageJPEGRepresentation方法替换jpegData(compressionQuality:)的旧API。

替换为:

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):
© www.soinside.com 2019 - 2024. All rights reserved.