简体   繁体   中英

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 . 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.

Using NSLog I found out that [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.

I am absolutely bamboozled. Please help >.< 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); ?> <?php print_r($_FILES); ?> returns Array {} .

  1. UIImageJPEGRepresentation(image, 1.0) returns a NSData object that can't be turned into a NSString. A jpeg does not consist of valid characters. 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. [NSData dataWithData:nil]; returns an empty NSData instance, not nil.

  3. Regarding your answer. nil is not a valid string encoding. 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.".

You can't convert postData back into a NSString. If you want to check that the image is part of the NSData object you could look at the length of 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? Please note that you can't convert it to UTF8 string using some category/extension as below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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