简体   繁体   English

NSData appendData:UIImageJPEGRepresentation()返回nil

[英]NSData appendData: UIImageJPEGRepresentation() returns nil

This has had me scratching for quite some time. 这让我抓了很长时间。

I am preparing a UIImage to post as part of a HTTP request . 我正在准备要发布的UIImage作为HTTP request一部分。 I am doing this with the following code: 我正在使用以下代码执行此操作:

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                        [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"q%@\"; filename=\".jpg\"\r\n" , key] dataUsingEncoding:NSUTF8StringEncoding]];
                        [postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                        [postData appendData:[NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]];
                        [postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

The problem I am having is that postData is null after running these lines. 我遇到的问题是运行这些行后postDatanull

Using NSLog I found out that [postData appendData:[NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]]; 我使用NSLog发现[postData appendData:[NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]]; is the line which is causing this to happen. 是导致这种情况发生的那条线。 I then went on to see if the image was a the problem. 然后,我继续查看图像是否有问题。 UIImageJPEGRepresentation(image, 1.0) outputs a mass of data to the console and adding image to a UIImageView shows the .jpg I was expecting. UIImageJPEGRepresentation(image, 1.0)将大量数据输出到控制台,并将图像添加到UIImageView中将显示我期望的.jpg。

I am absolutely bamboozled. 我绝对上当了。 Please help >.< S 请帮助>。<S

EDIT 编辑

After a bit of a debacle below I have realised that it is definitely posting the image . 在下面发生一些崩溃之后,我意识到它肯定是在发布图像。

NSLog(@"postData = %@",[postData length]);

shows 280861 (bytes?) but a PHP file with <?php print_r($_FILES); ?> 显示280861(字节?),但PHP文件带有<?php print_r($_FILES); ?> <?php print_r($_FILES); ?> returns Array {} . <?php print_r($_FILES); ?>返回Array {}

  1. UIImageJPEGRepresentation(image, 1.0) returns a NSData object that can't be turned into a NSString. UIImageJPEGRepresentation(image, 1.0)返回一个不能转换为NSString的NSData对象。 A jpeg does not consist of valid characters. jpeg不包含有效字符。 And arbitrary data can't be converted to a valid string. 而且任意数据都不能转换为有效字符串。

  2. Even if UIImageJPEGRepresentation(image, 1.0) returns nil, the [NSData dataWithData:] call makes sure that the data you append to postData is not nil. 即使UIImageJPEGRepresentation(image, 1.0)返回nil, [NSData dataWithData:]调用也可以确保您附加到postData的数据不是nil。 [NSData dataWithData:nil]; returns an empty NSData instance, not nil. 返回一个空的NSData实例,而不是nil。

  3. Regarding your answer. 关于你的答案。 nil is not a valid string encoding. nil不是有效的字符串编码。 The compiler should show a warning and you should see a warning in the console "Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.". 编译器应该显示警告,并且您应该在控制台中看到警告“检测到错误的NSStringEncoding值0x0000。假定NSASCIIStringEncoding。将在不久的将来停止这种兼容性映射行为。”。

You can't convert postData back into a NSString. 您不能将postData转换回NSString。 If you want to check that the image is part of the NSData object you could look at the length of postData. 如果要检查图像是否是NSData对象的一部分,可以查看postData的长度。

Resolved with: 解决:

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"%@.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n", key,key] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:imageData]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

The first newline after my already included data seems to have resolved this 我已经包含的数据之后的第一个换行符似乎已经解决了这个问题

How did you tell postData was null? 您如何得知postData为空? Please note that you can't convert it to UTF8 string using some category/extension as below. 请注意,您不能使用以下某些类别/扩展名将其转换为UTF8字符串。

extension NSData { func to_string(encoding:UInt = NSUTF8StringEncoding) -> String? { return NSString(data:self, encoding:encoding) as? String } }

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

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