views:

605

answers:

2

Hello, there is a error in my iPhone app. I use the CameraPicker to make and get a picture and then I give it to my methode sendPhoto, which will send the photo to TweetPhoto. Everything works great until I initialize an NSMutableDate with NSData. Here is the code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    NSData *imageData = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [self sendPhoto:(NSString*)@"lola" tweet:(BOOL)TRUE photo:(NSData*)imageData tags:(NSString*)@"" longitude:(float)1024 latitude:(float)512];
}

Here is the sendPhoto-methode:

- (void)sendPhoto:(NSString*)message tweet:(BOOL)tweet photo:(NSData*)photo tags:(NSString*)tags longitude:(float)longitude latitude:(float)latitude {
    NSString *url;
    if (tweet) {
        url = [NSString stringWithFormat:@"http://www.tweetphoto.com/uploadandpostapiwithkey.php"];
    } else {
        url = [NSString stringWithFormat:@"http://www.tweetphoto.com/uploadapiwithkey.php"];
    }

    NSString * boundary = @"tweetPhotoBoundaryParm";
    NSMutableData *postData = [NSMutableData dataWithCapacity:[photo length] + 1024];
    //!!!Here is the error!!!!                      
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString * userNameString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n%@", [prefs stringForKey:@"username_preference"]];
    NSString * passwordString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n%@", [prefs stringForKey:@"password_preference"]];
    NSString * apiString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n3eceaa7c-6d4d-41ab-be90-2a780e2d1961"];
    NSString * messageString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", message];
    NSString * tagsString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"tags\"\r\n\r\n%@", tags];
    NSString * latString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n%f", latitude];
    NSString * longString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n%f", longitude];
    NSString * boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
    NSString * boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];

    [postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[userNameString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[passwordString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[apiString dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
    //(...)
}

(The sendImage-methode is by http://davidjhinson.wordpress.com/2009/06/01/posting-photos-using-objective-c/)

The console also says this: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UIImage length]: unrecognized selector sent to instance 0x3b28df0'

Thanks for your help. Maybe there is just a stupid mistake in the code by me, but please don´t be angry, because I´m just learning programming for 2 1/2 weeks.

A: 

What exactly is the error you're getting? In other words, what makes you think there is an error on the line you've indicated?

If it crashes, what are the errors printed to the console?

Darren
The app just crashes in this line. I tried it with /* the code after this line and before and the app crashes just with this line. Of course I also tried it with breakpoints and in this line the app crashes.
Flocked
"Crash" can mean anything. The cause of the crash will be printed to your debugging console. Without that information it will be very difficult for anyone to determine the cause of the crash.
Darren
Oh and the console says this: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage length]: unrecognized selector sent to instance 0x3b28df0'
Flocked
Did I used the CameraPicker right `NSData *imageData = [info objectForKey:@"UIImagePickerControllerOriginalImage"];`? And can I get the lenght of it, like I did it in the sendPhoto-methode?
Flocked
+3  A: 

It sounds like you're passing a UIImage into this method, not an NSData chunk. Can you try logging the photo argument?

Ben Gottlieb
But I wrote: `NSData *imageData = [info objectForKey:@"UIImagePickerControllerOriginalImage"];`, is it wrong? How could/should I log the photo argument?
Flocked
Got it thanks to you. Just wrote this: `UIImage * images = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *imageData = UIImageJPEGRepresentation(images, 1);`
Flocked
for future reference, you can use NSLog(@"Photo: %@", photo);
Ben Gottlieb