如何在swift 3.0中使用Backendless上传图像

时间:2017-01-10 07:23:24

标签: swift3 uiimagepickercontroller backendless

我正在使用swift 3.0 uisng backendless。我是这个概念的新手。我正在使用UIImagePickerController上传我从电话库中选择的图像。在后面无尽的我正在使用Rest Api。我正在使用以下代码对图像进行上传..

 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)

    }
  

但我收到“状态代码错误= 400”。

任何人都可以告诉我,我在这里犯了什么错误。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

从第一步开始你就完全错了。 您提出的请求网址是在您成功上传文件后,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

更多细节,您可以在他们的文档中阅读。