从xcode

时间:2017-07-14 06:56:13

标签: php arrays xcode swift3 alamofire

我是初学者,我不知道如何使用alamofire将我的图像数组提交到我的数据库。下面是表单加载时的代码

    override func viewDidLoad()
        {
            super.viewDidLoad()

            // adding page image into the image square
            for i in 1...3
            {
                var imgView : UIImageView!
                imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
                imgView.contentMode = .scaleAspectFit
                imgView.tag = i
                imgView.image = UIImage(named: "page.png")
                imageArray.add(imgView)
            }

            videoView.isHidden = true
            photoView.type = iCarouselType.coverFlow2
            photoView.reloadData()
        }

用户选择/拍照后,图片将替换page.png ..一切正常。以下是替换代码

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        self.selectedImageView.image = chosenImage

        imageArray.replaceObject(at: self.selectedImageView.tag-1, with: self.selectedImageView)

        photoView.reloadData()

        dismiss(animated:true, completion: nil)
    }

至于提交部分,我在理解数组时遇到了麻烦,还有什么提交数组到php / database。请帮助我,因为我不知道如何从这里继续。我需要提交图像数组(最多3张图像)以及表格中的其他参数。

@IBAction func submitAction(_ sender: Any)
    {

            let URL_Submit = "http://localhost/form.php"
            let parameters: Parameters=[

                "block":blockText.text!,
                "category":catText.text!,
                "description":descText.text!]


            Alamofire.request(URL_Submit, method: .post, parameters: parameters).responseJSON
                {

                    response in
                    //printing response
                    print(response)

                    let alertController = UIAlertController(title: "Success!", message:
                        "Your feedback has been submitted", preferredStyle: UIAlertControllerStyle.alert)

                    alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default,handler: nil))

                    self.present(alertController, animated: true, completion: nil)

                }

           }

我删除了我在submitAction bc中上传图片的尝试。请帮助我,因为我现在还没有任何线索继续。谢谢你的时间

1 个答案:

答案 0 :(得分:0)

您必须使用分段上传将文件上传到服务器,例如此处是使用alamofire上传4张图片的示例。您也可以创建一个数组并上传多个图像:

func uploadImagesAndData(params:[String : AnyObject]?,image1: UIImage,image2: UIImage,image3: UIImage,image4: UIImage,success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void) {

let imageData1 = UIImageJPEGRepresentation(image1, 0.5)!
let imageData2 = UIImageJPEGRepresentation(image2, 0.5)!

let imageData3 = UIImageJPEGRepresentation(image3, 0.5)!

let imageData4 = UIImageJPEGRepresentation(image4, 0.5)!


Alamofire.upload(multipartFormData: { multipartFormData in

    for (key, value) in params! {
        if let data = value.data(using: String.Encoding.utf8.rawValue) {
            multipartFormData.append(data, withName: key)
        }
    }

    //### Here "file" is the name of your image parameter ###//

    multipartFormData.append(imageData1, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
    multipartFormData.append(imageData2, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
    multipartFormData.append(imageData3, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
    multipartFormData.append(imageData4, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")

},
                 to: K_BASEURL + K_API_LOGINDATA, encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let upload, _, _):
                        upload
                            .validate()
                            .responseJSON { response in
                                switch response.result {
                                case .success(let value):
                                    print("responseObject: \(value)")


                                    let resp = JSON(response.result)
                                    success(resp)
                                case .failure(let responseError):
                                    print("responseError: \(responseError)")
                                    failure(responseError)
                                }
                        }
                    case .failure(let encodingError):
                        print("encodingError: \(encodingError)")
                    }
 })
}

在您的情况下,您可以循环浏览array。我的后端已thumb[]检查,因此我将其命名为"thumb[]"。你可以有一个不同的名字:

  for thumbImg in thumbImagesArray {
            multipartFormData.append(UIImageJPEGRepresentation(thumbImg as! UIImage, 1)!, withName: "thumbs[]", fileName: "thumbs.jpeg", mimeType: "image/jpeg")
        }