简体   繁体   English

无法使用NSData(包含png)初始化NSMutableData

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

there is a error in my iPhone app. 我的iPhone应用程序中有错误。 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. 我使用CameraPicker制作并获取图片,然后将其提供给我的方法sendPhoto,该方法会将照片发送到TweetPhoto。 Everything works great until I initialize an NSMutableDate with NSData. 在我使用NSData初始化NSMutableDate之前,一切工作都很好。 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: 这是sendPhoto方法:

- (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/ ) (sendImage方法是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' 控制台也这样说: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[UIImage长度]:无法识别的选择器发送到实例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. 也许我的代码中只有一个愚蠢的错误,但是请不要生气,因为我只是在学习编程2 1/2周。

It sounds like you're passing a UIImage into this method, not an NSData chunk. 听起来您正在将UIImage而不是NSData块传递给此方法。 Can you try logging the photo argument? 您可以尝试记录照片参数吗?

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? 如果崩溃,打印到控制台的错误是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM