2017-01-10 2 views
0

나는 백엔드없이 스위프트 3.0을 개발 중이다. 저는이 개념을 처음 접했습니다. UIImagePickerController를 사용하여 전화 갤러리에서 선택한 이미지를 업로드하고 있습니다. 백 끝없이 나는 휴식 API를 사용하고 있습니다. 나는스위프트 3.0에서 백엔드리스를 사용하여 이미지를 업로드하는 방법

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    { 
     let image = info[UIImagePickerControllerOriginalImage] as? UIImage 
     self.uploadButton.isHidden = true 
     myImageView.contentMode = .scaleAspectFit 
     myImageView.image = image 


     let imageUrl   = info[UIImagePickerControllerReferenceURL] as! NSURL 
     let imageName   = imageUrl.lastPathComponent 
     let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
     let photoURL   = NSURL(fileURLWithPath: documentDirectory) 
     let localPath   = photoURL.appendingPathComponent(imageName!) 

     print(localPath!) 


     let imageurl = info[UIImagePickerControllerReferenceURL] as? NSURL 
     let imagename = imageurl?.lastPathComponent 
     print(imagename!) 

     //https://api.backendless.com/<application id>/<version name>/files/<path>/<file name> 

     Alamofire.request(“https://api.backendless.com/66B90F83-A813-84CF-FF9D-4A01AC28E100/v1/files/ + "\(localPath!)/\(imagename!)", method: .post, parameters: nil, encoding: JSONEncoding.default, headers: HeadersClass.allHeaders.headers).responseJSON { response in 
      debugPrint(response) 
     } 

     imagePicker.dismiss(animated: true, completion: nil) 

    } 

.. 다음 코드를 사용하여 이미지를 uplaoding하고 그러나 나는 "상태 코드를 오류 = 400"를 얻고있다.

누구든지 내가 여기서 한 실수를 말해 줄 수 있습니까? 미리 감사드립니다.

+0

왜 라이브러리를 사용하지 않습니까? https://backendless.com/documentation/files/ios/files_file_upload.htm –

+0

나는 단지 400 개의 상태 코드 오류 –

+0

을 얻지 못하는 경우에만 해당 Api를 사용했습니다. 그렇지 않은 주소를 요청하여 alamofire를 통해 보내려고합니다. 백엔드 서버가 파일 경로에 대해 어떻게 알아야합니까? 그들이 파일을 업로드 한 것을 사용하면, 그 부분을 가지고 있습니다 –

답변

0

첫 번째 단계에서 완전히 잘못되었습니다. 요청한 url은 파일을 성공적으로 업로드 할 때 백엔드없는 서비스가 사용자에게 반환하는 return url입니다.

는 backendless 서비스, https://backendless.com/documentation/files/ios/files_file_upload.htm에 대해 당신은 아래 여기에 나열된 기능을 구현해야합니다

// Upload data block identified as 'content' to the specified path. 
// If the server returns an error, it is delivered through 
// the 'responder' object 
func upload(_ path: String, 
      content content: NSData, 
      responder responder: IResponder!) -> Void 

// Upload data block identified as 'content' to the specified path. 
// If the file already exists on the server, overwrite if the 
// 'overwrite' argument is set to YES/TRUE. 
// If the server returns an error, it is delivered through 
// the 'responder' object 
func upload(_ path: String, 
      content content: NSData, 
      overwrite overwrite: Bool, 
      responder responder: IResponder!) -> Void 

// Upload data block identified as 'content' to the specified path. 
// If the server returns an error, it is delivered through 
// the 'error' block-based callback 
func upload(_ path: String, 
      content content: NSData, 
      response responseBlock: ((BackendlessFile!) -> Void)!, 
      error errorBlock: ((Fault!) -> Void)!) -> Void 

// Upload data block identified as 'content' to the specified path. 
// If the file already exists on the server, overwrite if the 
// 'overwrite' argument is set to YES/TRUE. 
// If the server returns an error, it is delivered through 
// the 'error' block-based callback 
func upload(_ path: String, 
      content content: NSData, 
      overwrite overwrite: Bool, 
      response responseBlock: ((BackendlessFile!) -> Void)!, 
      error errorBlock: ((Fault!) -> Void)!) -> Void 

세부 사항은 더, 당신은 자신의 문서에서 읽을 수 있습니다.

+0

"업로드 기능"을 사용했지만 어떻게 보낸 이미지를 볼 수 있습니까? –

관련 문제