简体   繁体   English

如何使用Swift 2.0在Parse.com(REST API)中上传和关联图像

[英]How to upload and associate an Image with a User in Parse.com (REST API), using Swift 2.0

I am trying to associate an image or a file with an object in Parse.com using the REST API. 我正在尝试使用REST API将图像或文件与Parse.com中的对象相关联。 The Parse.com REST API Doc is quite vague, it talks about first how to upload which is fine, and then how to associate. Parse.com REST API Doc非常模糊,它首先讨论如何上传哪个很好,然后如何关联。 The only issue is that it doesn't show how to associate with a User table , only an Object table , so when I tried to associate with a user, it asked for a username and password, and the response is as if it tries to create a new user . 唯一的问题是它没有显示如何与User table关联,只显示一个Object table ,所以当我尝试与用户关联时,它要求输入用户名和密码,响应就好像它试图创建一个新user When I tried to associate with a regular table Company , it create a new entry. 当我尝试与常规表Company关联时,它会创建一个新条目。 Any help would welcome, this is the code I have so far. 欢迎任何帮助,这是我到目前为止的代码。

This is the code to upload a file to Parse.com with REST 这是使用REST将文件上传到Parse.com的代码

    let baseURL = NSURL(string: self.baseURL)
    let url = NSURL(string: "/1/files/pic.jpg", relativeToURL: baseURL)

    let request = NSMutableURLRequest()
    request.HTTPMethod = "\(HTTPMethod.POST)"
    request.addValue(appID, forHTTPHeaderField:  "X-Parse-Application-Id")
    request.addValue(apiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
    request.addValue("image/jpeg", forHTTPHeaderField: "Content-Type")

    let image = UIImage(named: "empAvatar")

    let imageData = UIImageJPEGRepresentation(image!, 0.9)
    let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

    let param = ["userProfile":base64String]

    do{
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(param, options: .PrettyPrinted)
    } catch {
        print("ERROR: HTTP Body JSON")
    }

    request.URL = url
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) {
        data, response, error in
        do {
            let imageDic = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
            print("DATA: \(imageDic)")
        } catch {

        }
    }
    task.resume()

This is the code to associate a file with a user/object 这是将文件与用户/对象相关联的代码

let baseURL = NSURL(string: self.baseURL)
    let url = NSURL(string: "/1/users/", relativeToURL: baseURL)

    let request = NSMutableURLRequest()
    request.HTTPMethod = "\(HTTPMethod.POST)"
    request.addValue(appID, forHTTPHeaderField:  "X-Parse-Application-Id")
    request.addValue(apiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")


   let param = ["name":"John", "picture":["name":"tfss-127e50c4-be6e-4228-b1a3-3f253358ac-pic.jpg","__type":"File"]]


    do{
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(param, options: .PrettyPrinted)
    } catch {
        print("ERROR: HTTP Body JSON")
    }

    request.URL = url
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) {
        data, response, error in
        do {
            let imageDic = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
            print("DATA: \(imageDic)")
        } catch {

        }
    }
    task.resume()

With the URL, I also tried: 通过URL,我也尝试过:

 let url = NSURL(string: "/1/class/Company/", relativeToURL: baseURL)

And it just created a new entry. 它刚刚创建了一个新条目。

Thanks you! 谢谢!

POST请求将创建新的条目尝试使用PUT方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM