Search code examples
iphoneobjective-cinitializationnsdatansmutabledata

Can't initialize an NSMutableData with an NSData (which contains a png)


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.


Solution

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