从图库中拍摄照片并将图像上传到服务器swift 4

时间:2018-10-20 06:24:23

标签: php ios swift image swift4

我是新手,尝试过许多博客文章和github,以从库中获取图像并将其上传到服务器(php服务器端)。您能否编写从照片库中获取照片并将其上传到使用alamofire和其他任何方法的服务器上的代码。

下面是我试图从照相馆上传照片的代码

 @IBAction func upload(_ sender: Any) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        picker.allowsEditing = false
        self.present(picker, animated: true) {

        }

    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

     if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            imageView.image = originalImage
        let image = UIImage.init(named: "\(originalImage)")
        let imgData = UIImageJPEGRepresentation(image!, 0.2)!
        let parameters = ["user":"Sol", "password":"secret1234"]


        Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            } //Optional for extra parameters
        },
                         to:"website")
        { (result) in
            switch result {
            case .success(let upload, _, _):

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

                upload.responseJSON { response in
                    print(response.result.value)
                }

            case .failure(let encodingError):
                print(encodingError)
            }
        }
        }
 self.dismiss(animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

@IBAction func upload(_ sender: Any) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        picker.allowsEditing = false
        self.present(picker, animated: true) {

        }

    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            imageView.image = originalImage
            if let imgData = UIImageJPEGRepresentation(originalImage, 0.2) {
                let parameters = ["user":"Sol", "password":"secret1234"]
                upload(params: parameters, imageData: imgData)
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }

    func upload(params : [String: Any], imageData: Data) {

        if let url = URL(string: "EnterUrl") {
            Alamofire.upload(
                multipartFormData: { (multipartdata) in

                    multipartdata.append(
                        imageData,
                        withName: "fileset",
                        fileName: String("\(Date().timeIntervalSince1970).jpg"),
                        mimeType: "image/jpg"
                    )

                    for (key,value) in params {
                        if let data = value as? String,
                            let data1 = data.data(using: .utf8)
                        {
                            multipartdata.append(
                                data1,
                                withName: key
                            )
                        }
                    }
            },
                to: url,
                method: .post,
                headers: nil,
                encodingCompletion: { (result) in
                    switch result {
                    case .success(let upload, _, _):
                        upload.responseJSON(completionHandler: { (response) in
                            if let err = response.error {
                                    print(err.localizedDescription)
                            } else {
                                print(response.result.value ?? "No data")

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

    }
}

尝试一下。