I can't upload image

ios

#1
func twitterSender(photoImported: UIImage) ->Void {
        let account = ACAccountStore()
        let accountType = account.accountTypeWithAccountTypeIdentifier(
            ACAccountTypeIdentifierTwitter)
        
        account.requestAccessToAccountsWithType(accountType, options: nil,
            completion: {(success: Bool, error: NSError!) -> Void in
                
                if success {
                    let arrayOfAccounts =
                    account.accountsWithAccountType(accountType)
                    
                    if arrayOfAccounts.count > 0 {
                        let twitterAccount = arrayOfAccounts.last as! ACAccount
                        var message = Dictionary<String, AnyObject>()
                        message["status"] = "My app test 5"
                        let imageData = UIImageJPEGRepresentation(photoImported, 0.9)
                        let imageString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
                        message["media_id_string"] = imageString
                        let requestURL = NSURL(string:
                            "https://api.twitter.com/1.1/statuses/update.json")
                        let postRequest = SLRequest(forServiceType:
                            SLServiceTypeTwitter,
                            requestMethod: SLRequestMethod.POST,
                            URL: requestURL,
                            parameters: message)
                
                        postRequest.addMultipartData(imageData, withName: "media_id", type: "POST media/upload", filename: "image.jpg")
                        postRequest.account = twitterAccount
                        
                        postRequest.performRequestWithHandler({
                            (responseData: NSData!,
                            urlResponse: NSHTTPURLResponse!,
                            error: NSError!) -> Void in
                            
                            if let err = error {
                                println("Error : \(err.localizedDescription)")
                            }
                            println("Twitter HTTP response \(urlResponse.statusCode)")
                        })
                    }
                }
        })
        
    }

I have asked this question early. But nobody answers me. I feel really helpless now. It already takes me so much time to figure out how to upload image to twitter. No I realize it seems like I need multipart post to upload image along with text. With this code above I will get Twitter HTTP response 200 as result. But when I check my twitter only text, no image. I really don’t know why and hope someone can help me.


#2
postRequest.addMultipartData(imageData, withName: "media_id", type: "POST media/upload", filename: "image.jpg")

That looks like it’s wrong. The media_id is expected to be an ID of a previously successfully uploaded image using the POST media/upload endpoint.
That means that you first need to do a POST to the media/upload endpoint using multipart/form-data encoding. If this is successful, you will get a response like the following:

{
  "media_id": 553639437322563584,
  "media_id_string": "553639437322563584",
  "size": 998865,
  "image": {
    "w": 2234,
    "h": 1873,
    "image_type": "image/jpeg"
  }
}

You need to use that media_id (or media_id_str if your JSON parser can’t handle large integers) and use it for the media_id value, when tweeting with the statuses/update endpoint. Note that this endpoint should not be multipart/form-data encoded.