如何使用Alamofire将多部分图像上传到服务器

时间:2018-04-26 11:00:42

标签: ios swift xcode alamofire multipartform-data

下面提到的是我将多部分图像上传到服务器的方法,但是当我尝试这样做时,应用程序崩溃了“意外发现nill”

但是当我从照片库中选择它时,图像的价值就在那里。

func createCoupon(_ code: String, storeID: Int, description: String, terms: String, image: UIImage, startDate: String, endDate: String, couponDiscount: String, minimumDiscount: String, percentage: String, maximumDiscount: String){
    let urlString = BaseURL + "create-coupon"
    let params =
        [
            "code"               :  code,
            "store_id"           :  storeID,
            "type"               :  "merchant",
            "description"        :  description,
            "terms"              :  terms,
            "start_date"         :  startDate,
            "end_date"           :  endDate,
            "coupon_discount"    :  couponDiscount,
            "minimum_total"      :  minimumDiscount,
            "percentage"         :  percentage,
            "maximum_discount"   :  maximumDiscount
            ] as [String : Any]

    let manager = Alamofire.SessionManager.default
    manager.session.configuration.timeoutIntervalForRequest = 30000
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(image, 0.2)!, withName: "image", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
        for (key, value) in params {
            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }
    }, to:urlString)
    {
        (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })
            upload.responseJSON { response in
                let resJson = response.result.value
                print(resJson)
                NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestSuccess.createCoupon.rawValue), object: nil, userInfo: ["data": resJson!])
            }

        case .failure(let encodingError):
            NotificationCenter.default.post(name: Notification.Name(rawValue: NotifRequestError.createCoupon.rawValue), object: encodingError, userInfo: nil)
        }
    }

6 个答案:

答案 0 :(得分:2)

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            }, to:"http://server1/upload_img.php")
        { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (Progress) in
                    print("Upload Progress: \(Progress.fractionCompleted)")
                })

                upload.responseJSON { response in
                    //self.delegate?.showSuccessAlert()
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.data)     // server data
                    print(response.result)   // result of response serialization
                    //                        self.showSuccesAlert()
                    //self.removeImage("frame", fileExtension: "txt")
                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }

            case .failure(let encodingError):
                //self.delegate?.showFailAlert()
                print(encodingError)
            }

        }

答案 1 :(得分:0)

只需简单使用此功能

/// Data for image
var userImageString = Data()

/// Convert Image to Data
func convertImageToData(image: UIImage) -> Data {            
     let imageData = UIImageJPEGRepresentation(image, 0.1)!
     return imageData
 }

 ///Conversion Method - with target size if required
 userImageString = convertImageToData(image: resizeImage(image: img, targetSize: CGSize.init(width: 600, height: 600)))

//MARK: Data to server
func imageDataToServer(parameters: [String : AnyObject])
{
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(self.userImageString, withName: "parameter_name", fileName: "image.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters
        {
            multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }

    }, to:"url", method: .post,
                     encodingCompletion: { encodingResult in
                         switch encodingResult {
                         case .success(let upload,_,_):

                             upload.responseJSON { (response:DataResponse<Any>) in
                                 print("RRRRR: \(response)")
                                 if response.result.isSuccess
                                 {
                                     /// Success
                                 }
                                 else
                                 {
                                     /// Failure
                                 }
                             }
                             upload.uploadProgress(closure: {
                                 progress in
                                 print(progress.fractionCompleted)
                             })
                         case .failure(let encodingError):
                             print(encodingError)
                         }
                     })
}

功能用途

如果你有参数发送参数else nil或[:]

self.imageDataToServer(parameters: parameters)

答案 2 :(得分:0)

使用以下代码,使用Alamofire进行单张图片上传  : Swift 3.0

var strImagePicked = [UIImage]() // declared as global variable ... this will be image picked from ImagePickerController

以下是用于从ImagePickerController上传单张图片的功能,并在您点击按钮上传Image时调用此方法

func imageuploadAPI(){

    let params =
    [
        "code"               :  code,
        "store_id"           :  storeID,
        "type"               :  "merchant",
        "description"        :  description,
        "terms"              :  terms,
        "start_date"         :  startDate,
        "end_date"           :  endDate,
        "coupon_discount"    :  couponDiscount,
        "minimum_total"      :  minimumDiscount,
        "percentage"         :  percentage,
        "maximum_discount"   :  maximumDiscount
        ] as [String : Any]

    Alamofire.upload(
        multipartFormData: { MultipartFormData in

            for (key, value) in params {
                MultipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }

            for element in 0..<self.strImagePicked.count {

                let singleImage : Data? = self.strImagePicked[element].lowestQualityJPEGNSData as Data?

                MultipartFormData.append(singleImage!, withName: "image", fileName: "swift_file.jpg", mimeType: "image/jpeg")

                //"image" is the parameter for image 
            }
    }, to: "your_URL") { (result) in

        switch result {
        case .success(let upload, _, _):

            upload.responseJSON { response in
                print(response.result.value as Any)

            }
        case .failure(let encodingError):
             print(encodingError)
            break
        }
    }
}

// extension is used for image so that if image will be of hight quality it will reduce it's size
extension UIImage {

var highestQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 1.0)! as NSData }
var highQualityJPEGNSData: NSData    { return UIImageJPEGRepresentation(self, 0.75)! as NSData}
var mediumQualityJPEGNSData: NSData  { return UIImageJPEGRepresentation(self, 0.5)! as NSData }
var lowQualityJPEGNSData: NSData     { return UIImageJPEGRepresentation(self, 0.25)! as NSData}
var lowestQualityJPEGNSData: NSData  { return UIImageJPEGRepresentation(self, 0.0)! as NSData }

}

答案 3 :(得分:0)

在这种情况下,我创造了一个满足你需求的功能。将UIImage传递给&#39;图像&#39;以下请求中的参数。

multipartUploadRequestWith(imageData: UIImageJPEGRepresentation(image, 1.0), parameters: params, onCompletion: {
        print("Upload Successful")
    }) { (error) in
        print(error.debugDescription)
}

以下是该功能的代码。

func multipartUploadRequestWith(imageData: Data?, parameters: [String : Any], onCompletion: (() -> Void)? = nil, onError: ((Error?) -> Void)? = nil){

    let url =  /* your API url */

    let headers: HTTPHeaders = [
        /* "Authorization": "your_access_token",  in case you need authorization header */
        "Content-type": "multipart/form-data",
    ]
    print(headers)
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in parameters {
            multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
        }

        if let data = imageData{
            multipartFormData.append(data, withName: "photo", fileName: "photo.jpeg", mimeType: "image/jpeg")
        }

    }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.response { response in
                if let err = response.error{
                    print("This error")
                    let alert = UIAlertController(title: "Upload Failed", message: "The image upload failed. Check your internet connection", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                    UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
                    onError?(err)
                    return
                }
                if response.response?.statusCode == 200 {
                    print("Succesfully uploaded")
                    onCompletion?()
                } else {
                    print("Error " )
                }
            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")
            onError?(error)
        }
    }
}

答案 4 :(得分:0)

尝试一下

func callAPI( _ parameters:[String:Any] , url:String) {


        print(parameters)

        let uploadImageProfileURL = url

        var urlRequest = URLRequest(url: URL(string:uploadImageProfileURL)!)
        urlRequest.httpMethod = "POST"

        urlRequest.allHTTPHeaderFields = [
            "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
            "cache-control": "no-cache",
        ]

        Alamofire.upload(multipartFormData: { multipartFormData in

            for (key, value) in parameters {
                if value is String {
                    multipartFormData.append((value as! String).data(using: String.Encoding.utf8)!, withName: key )
                }

                if value is UIImage {

                    let imgData = UIImageJPEGRepresentation(value as! UIImage, 1)!

                    multipartFormData.append(imgData, withName: key  ,fileName: "file.jpg", mimeType: "image/jpg")
                }

                if value is Array<UIImage> {

                    let arrayValue = value as! [UIImage]

                    for image in arrayValue {

                        let fileName = "file\(arc4random()%1000).jpg"
                        let mimeType = "application/octet-stream" ?? "image/jpg"
                        let data = UIImageJPEGRepresentation(image, 1)

                        multipartFormData.append(data!, withName: "\(key)", fileName:fileName, mimeType: mimeType)
                    }

                }

            }},to:url,method: .post)
        { (result) in

            switch result {

            case .success(let upload, _, _):

                upload.responseJSON { response in

                    if let JSON = response.result.value {

                    }
                }




            case .failure(let encodingError):
               print(encodingError.localizedDescription)
            }

        }
    }

答案 5 :(得分:0)

Alamofire4和Swift 4.1

第一步:拍摄一个imageView并使用imagepickerview拾取图像 之后,点击一个按钮,将在其中调用以下功能

**

func uploadAvatar() {
            let id : String = UserDefaults.standard.value(forKey: "id") as! String
            let token : String = UserDefaults.standard.value(forKey: "token") as! String
            let url = "http://203.163.248.214:1357/api/user/update_image"
            let headers = ["x-access-token": token]
            showLoader(OnView: view)

        let imageData = self.imgView.image!.jpegData(compressionQuality: 0.7)

        let parameters: [String : AnyObject] = ["user_id": id as AnyObject]

            Alamofire.upload(multipartFormData:{ multipartFormData in
                multipartFormData.append(imageData!, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
                    for (key, value) in parameters
                    {
                        multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                    }
            }, to: url, headers: headers)
            { (result) in
                switch result {
                case .success(let upload,_,_ ):
                    upload.uploadProgress(closure: { (progress) in
                        //Print progress
                    })
                    upload.responseJSON
                        { response in
                            let json = response.result.value as! [String:AnyObject]
                            print(json)
                            if response.result.value != nil
                            {
                                if json["status"] as! NSInteger == 200 {

                                    DispatchQueue.main.async(execute: {() -> Void in

                                        self.dismissLoader()
                                        self.userImage = json["data"] as! [String:AnyObject]

                                    })

                                }else{
                                    self.dismissLoader()
                                }
                            }
                    }
                case .failure( _):
                    self.dismissLoader()
                    break
                }
            }
        }*

*

userImage将是ur数组,或者可能是字典,具体取决于响应并将其全局声明为 var userImage =字符串:AnyObject