【问题标题】:Can't convert NSData to NSString when using UIImageJPEGRepresentation()?使用 UIImageJPEGRepresentation() 时无法将 NSData 转换为 NSString?
【发布时间】:2015-08-17 21:49:24
【问题描述】:

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

我已按照此链接将 NSData 转换为 NSString

Convert UTF-8 encoded NSData to NSString

这是我的代码:-

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

我检查了所有变量,它们是正确的,如下

(只有变量“str”不应该为nil。)

最后我得到的正文字符串如下:-

bodyData =

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

(null)
--tqb3fjo6tld9--

【问题讨论】:

  • 您不能直接将NSData 转换为NSString,因为数据不代表字符串。您可能需要使用 base64 编码将其转换为字符串。
  • @rmaddy:我点击了这个链接stackoverflow.com/questions/2467844/…
  • 该链接与您的需求无关。那是关于包含 UTF-8 编码字符串值的数据。您的数据不是编码的字符串值,而是图像数据。巨大的差异。查看NSData 的文档,了解获取base64 编码的数据字符串的方法。
  • 或者,您可以将请求正文创建为可变数据,并将其附加到与您相同的序列转换为数据的字符串

标签: ios objective-c uiimagejpegrepresentation


【解决方案1】:

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

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

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

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

更新

在 Swift 中,使用 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 中,使用 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)

【讨论】:

  • 嗨,罗伯。感谢那;如果你看到这个,我一直在想.. 现在在 Data 而不是 NSData 的时代,你到底是如何“转换”一个 jpeg 表示(正如你所解释的,一个二进制数据)转换为 UTF-8 字符串(例如,以 http 形式使用)...... ??!?
  • 请注意,如果您有一个 Data x 并且您使用 append 将一些字符串作为 utf8 添加到 x 中,那么这令人难以置信。 然后你添加一个“真实的”二进制数据,例如 jpeg 表示到 x - 实际上它没有“x” - 哎哟!
  • 确实我问了这个问题:stackoverflow.com/questions/43808693/…
猜你喜欢
  • 1970-01-01
  • 2014-10-14
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多