views:

4222

answers:

2

There's a similar question on here about this which I've read and I've tried to follow the advice given there... and I think I'm 95% complete but the remaining 5%... well you know ;-)

So I'm trying to call the twitPic API to upload an image and I've got the image contained in a UIImageView which I'm displaying on the screen (I can see it so it's definitely there). My code to form the API call looks like this:

    NSURL *url = [NSURL URLWithString:@"http://twitpic.com/api/upload"];
NSString *username = @"myUsername";
NSString *password = @"myPassword";
NSData *twitpicImage = UIImagePNGRepresentation(imageView.image);

// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];

[request setPostValue:twitpicImage forKey:@"media"];
[request setPostValue:username forKey:@"username"];
[request setPostValue:password forKey:@"password"];

// Initiate the WebService request
[request start];

I get an error back from it stating 'image not found'.

Is it obvious what I'm doing wrong? Any hints at all? I'm only a week deep in ObjectiveC so it's quite likely it's a real newbie error.

On the same track - it's not clear to me how I can capture a success or failure properly in code here - I'm currently dumping the 'request responseString' to an alert which isn't the best thing - how can I check the result properly?

I've also seen the use of 'NSLog' - which I suspect is a debugging/console logging tool - but I can't see the output from this anywhere in XCode - it doesn't SEEM to be shown in the debugger - any clues at all?!

Sorry if the above is really dumb - I can take a little ridicule - but I'm kind of isolated with my iPhone adventures - no one to bounce anything off etc - so I'm venting it all off here ;-)

Cheers,

Jamie.

+12  A: 

Hi Jamie,

You need to use the setData method to copy the image data into the post, like this:

[request setData:twitPicImage forKey:@"media"];

You're making a synchronous call, which is going to stall your app while you upload all that image data - you might want to switch to using an NSOperationQueue, or the ASINetworkQueue subclass that allows you to show a progress bar.

You should be able to see NSLog output in the debugger window of XCode. Make sure you've switched to this (control top left with a spray can on). You can also launch the console.

Jane Sales
+1  A: 

Thanks Jane - extremely helpful! I'll give this a shot tonight when I return from the day job ;-)

I'll vote your response up once I have a reputation (hopefully good!) on here...

Cheers,

Jamie.

badmanj