简体   繁体   中英

want to upload image through php api in swift 4

I want to send image to php api as a link or any method so that php code to save this into a folder directory. This is my image picking code.

 @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickerImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let extURL = info[UIImagePickerControllerImageURL] as! NSURL

        print("image url \(extURL)")
        let ext = extURL.lastPathComponent
        imageURL = ext!
            self.imgRef.contentMode = .scaleAspectFit
            self.imgRef.image = pickerImage  
    }
    picker.dismiss(animated: false, completion: nil)
}

how I can get image send to this via php api to store into server folder

Use this Alamofire method:

func hitMultipartForImage(_ params:Dictionary<String,String>,image:UIImage?,imageParamName : String?,unReachable:(() -> Void),handler:@escaping ((Dictionary<String,Any>?) -> Void)) {
        print_debug("Params:  \(params)")
        let BASE_URL = "http:" //Your end point
        var imageParam = "image"
        if imageParamName ?? "" != ""{
            imageParam = imageParamName!
        }

        Alamofire.upload(
            multipartFormData: { multipartFormData in
                for (key, value) in params {
                    if "appendURL" != key {
                        multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
                    }
                }
                if image != nil{
                    let imgData = UIImageJPEGRepresentation(image!, 0.5)
                    if imgData != nil {
                        print_debug("img data available")
                        multipartFormData.append(imgData!, withName: imageParam, fileName: "file.png", mimeType: "image/png")
                    }
                }
        },
            to: BASE_URL,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in

                        switch response.result {
                        case .success:
                            if let jsonDict = response.result.value as? Dictionary<String,Any> {
                                print_debug("Json Response: \(jsonDict)") // serialized json response
                                handler(jsonDict)
                            }
                            else{
                                handler(nil)
                            }
                            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                                print("Server Response: \(utf8Text)") // original server data as UTF8 string
                            }
                            break
                        case .failure(let error):
                            handler(nil)
                            print_debug(error)
                            break
                        }
                    }
                case .failure(let encodingError):
                    print(encodingError)
                }
        }
        )
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM