2017-07-14 47 views
0

到PHP我是一個初學者,我不知道如何去使用alamofire提交我的形象的陣列到我的數據庫。下面是我的代碼時,它的形式加載上傳與其他參數圖像的陣列在Xcode

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 /數據庫的麻煩。請幫助我,因爲我不知道如何從這裏繼續。我需要提交圖像數組(最多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它不工作。請幫助我,因爲我現在還沒有任何線索可以繼續。感謝您的時間

+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") 
     } 
+0

我沒有得到你,你可以請更新代碼,所有的錯誤使用礦代碼後,你得到了你的問題? –