0

I am creating a multipart image data upload request. i am getting NSData from UIImage by UIImageJPEGRepresentation but when i try to convert the data into string i am getting nil value

i have followed this link to convert NSData to NSString

Convert UTF-8 encoded NSData to NSString

here is my code :-

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    NSMutableString *body = [NSMutableString string];
    [body appendFormat:@"--%@\r\n", boundary];
    int i =0;
    for (UIImage *image in imagesArray) {
        NSData *data = UIImageJPEGRepresentation(image, 1.0);
        [body appendFormat:@"Content-Disposition:form-data; name=\"userfile[]\"; filename=\"image%d.jpeg\"\r\n",i];
        [body appendFormat:@"Content-Type: image/*\r\n\r\n"];
        NSString *str = [NSString stringWithUTF8String:[data bytes]]; //this variable should not be nil
        [body appendFormat:@"%@",str];
        if (error)
        {
            NSLog(@"%@", error);
        }
        i++;
    }
    [body appendFormat:@"\r\n--%@--\r\n", boundary];

    NSLog(@"bodyData = %@",body);

I have checked all the variables and they are correct, and are as follows

(only variable "str" should not be nil.)

enter image description here

enter image description here

enter image description here

enter image description here

and finally i am getting the body string as follows :-

bodyData =

--tqb3fjo6tld9
Content-Disposition:form-data; name="userfile[]"; filename="image0.jpeg"
Content-Type: image/*

(null)
--tqb3fjo6tld9--
Community
  • 1
  • 1
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
  • 2
    You can't directly convert `NSData` to an `NSString` since the data doesn't represent a string. You probably need to convert it to a string using base64 encoding. – rmaddy Jun 03 '15 at 15:22
  • @rmaddy : i followed this link http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring – Pawan Joshi Jun 03 '15 at 15:24
  • 2
    That link has nothing to do with what you need. That was about data containing a UTF-8 encoding string value. Your data isn't an encoded string value, it's image data. HUGE difference. Look at the docs for `NSData` for a way to get a base64 encoded string of the data. – rmaddy Jun 03 '15 at 15:27
  • alternatively you can create request body as mutable data and append it with strings converted to data in the same sequence as you do – sage444 May 05 '17 at 15:22

1 Answers1

5

Your mistake is thinking that the JPEG representation of an image is a UTF-8-encoded string. It's not. It's arbitrary binary data. UTF-8 strings aren't arbitrary sequences of bytes; there are rules about what bytes can follow other bytes.

You want to encode your JPEG data as a string suitable for use in an HTTP form submission. One way is to use base64 encoding, like this:

    [body appendString:@"Content-Type: image/*\r\n"];
    [body appendString:@"Content-Transfer-Encoding: base64\r\n\r\n"];
    [body appendString:[data base64EncodedStringWithOptions: NSDataBase64Encoding76CharacterLineLength]];

On the other hand, you could use the binary encoding and include the raw JPEG data directly. In that case, you would need to change the type of body to NSMutableData and build it up with different methods, as you cannot append arbitrary binary data to an NSMutableString.

UPDATE

In Swift, with an NSMutableString:

let data = UIImageJPEGRepresentation(image, 0.5)!
let body = NSMutableString()
body.append("Content-Type: image/*\r\n")
body.append("Content-Transfer-Encoding: base64\r\n\r\n")
body.append(data.base64EncodedString(options: .lineLength76Characters))

In Swift, with a String:

let data = UIImageJPEGRepresentation(image, 0.5)!
var body = ""
body += "Content-Type: image/*\r\n"
body += "Content-Transfer-Encoding: base64\r\n\r\n"
body += data.base64EncodedString(options: .lineLength76Characters)
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Hi Rob. Thanks for that; if you see this, I've been wondering .. now in the era of `Data` rather than `NSData`, how the heck **do** you "convert" a jpeg representation (as you explain, a binary data) in to UTF-8 string (for use, for example, in a http form) ......... ??!? – Fattie May 05 '17 at 15:09
  • Note that, incredibly confusingly, if you have a `Data` x and you add to x a few strings as utf8 using `append`. *And then you add a 'real' binary data, such as a jpeg representation* to x - in fact it nils "x" - ouch! – Fattie May 05 '17 at 15:11
  • Indeed I asked this as a question: http://stackoverflow.com/questions/43808693/with-data-not-nsdata-in-fact-how-actually-do-you-make-a-utf8-version-of-a-jpe – Fattie May 05 '17 at 15:30