使用Data(而非NSData),实际上您实际上是如何制作uteg8版本的jpeg的? [英] With Data (not NSData), in fact how actually do you make a utf8 version of a jpeg?

查看:157
本文介绍了使用Data(而非NSData),实际上您实际上是如何制作uteg8版本的jpeg的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此处所述, https://stackoverflow.com/a/30624755/294884

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

[It is a mistake to think 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.

实际上,在Data时代,如何最好地从图像制作utf8数据?

In fact, how do you best make a utf8 data from an image, in the Data era?

日常使用案例均采用http格式.

The everyday use case is in a http form.

我做这样的事情,

    formData.append( "Content-Disposition etc".(using: .utf8)! )
    formData.append( "Content type etc".(using: .utf8)! )

    // now let's add a #@@%$%$% image

    let trueBinary = UIImageJPEGRepresentation(i, 0.10)!
    print("the size seems to be \(trueBinary.count)")
    
    let mystere = trueBinary.base64EncodedString(
                  options: .lineLength76Characters)
    
    formData.append( mystere.data(using: .utf8)! )

但是

请注意,令人难以置信的是,如果您有一个Data x,并且使用append ....向其中添加了一些字符串作为utf8 ....然后,您添加了一个真实的"二进制数据,例如jpeg表示x ... ...... 实际上,它不表示"x". -哎呀!(它不只是不添加二进制数字1",它还可以使x完全消失.)

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! (It doesn't just "not add the binary one", it scratches x to nothing.)

推荐答案

此行有问题:

let mystere = trueBinary.count.base64EncodedString(
              options: .lineLength76Characters)

问题是trueBinaryDatatrueBinary.countInt,而Int没有base64EncodedString方法.您想要这个:

The problem is that trueBinary is a Data, and trueBinary.count is an Int, and Int doesn't have a base64EncodedString method. You want this:

let mystere = trueBinary.base64EncodedString(
              options: .lineLength76Characters)

关于线长"概念":如果不想,则不必指定行长选项.可以说我不应该在问题中提到的答案中指定一个,因为 HTTP请求不是严格的MIME消息不受MIME行长的限制限制.您可以改为:

As for ‘the "lineLength" concept’: you don't have to specify a line length option if you don't want to. Arguably I shouldn't have specified one in the answer you referenced in your question, because HTTP requests are not strictly MIME messages and are not subject to the MIME line length limit. You can do this instead:

let mystere = trueBinary.base64EncodedString()

这篇关于使用Data(而非NSData),实际上您实际上是如何制作uteg8版本的jpeg的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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