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.)
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--
NSData
to anNSString
since the data doesn't represent a string. You probably need to convert it to a string using base64 encoding. – rmaddyNSData
for a way to get a base64 encoded string of the data. – rmaddy