Upload image with Swift and custom request to Twitter


#1

Hi there,

I tried to use POST upload/media API in my app but it looks like I’m stuck with this `-[NSConcreteData _fastCharacterContents]:

error and I don’t know what to do to fix it. Here’s the code I use :
let uploadMediaUrlString = "https://upload.twitter.com/1.1/media/upload.json" let imgData = UIImagePNGRepresentation(self.albumImg.image) let params = ["media":imgData] var mediaError : NSError? let mediaRequest = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST", URL: uploadMediaUrlString, parameters: params as [NSObject : AnyObject], error: &mediaError)

The error happen for params where I can’t use it as a <String,NSData> dictionary I don’t know why.

Thanks in advance for your response :wink:


#2

Hey Kevin,

I believe you need to do something like this:

    let image = UIImage(named: "foo.jpg")
    let imageData = UIImageJPEGRepresentation(image, 0.9)
    let imageString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)        
    var requestError : NSError?
    
    let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST", URL: "https://upload.twitter.com/1.1/media/upload.json", parameters: ["media":imageString], error: &requestError)

V


#3