使用UIImageJPEGRepresentation()时无法将NSData转换为NSString? [英] Can't convert NSData to NSString when using UIImageJPEGRepresentation()?

查看:165
本文介绍了使用UIImageJPEGRepresentation()时无法将NSData转换为NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个多部分图片数据上传请求。
i通过UIImageJPEGRepresentation
从UIImage获取NSData但是当我尝试将数据转换为字符串时我得到零值

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

我已经关注此链接将NSData转换为NSString

i have followed this link to convert NSData to NSString

将UTF-8编码的NSData转换为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

(只有变量str不应该是nil。)

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


推荐答案

您的错误是认为图像的JPEG表示是UTF-8编码的字符串。不是。这是任意的二进制数据。 UTF-8字符串不是任意字节序列;有关于哪些字节可以跟随其他字节的规则。

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.

您希望将JPEG数据编码为适合在HTTP表单提交中使用的字符串。一种方法是使用base64编码,如下所示:

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]];

另一方面,你可以使用二进制编码并直接包含原始JPEG数据。在这种情况下,您需要将 body 的类型更改为 NSMutableData 并使用不同的方法构建它,如你不能将任意二进制数据附加到 NSMutableString

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.

在Swift中,使用 NSMutableString

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

在Swift中,字符串

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)

这篇关于使用UIImageJPEGRepresentation()时无法将NSData转换为NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆