Base64 Encoding/Decoding with Swift 2

跟風遠走 提交于 2019-12-04 12:35:12

问题


My code was working well on Xcode 6.4 with Swift 1.2:

 var imageData = UIImageJPEGRepresentation(firstImageView.image!, 0.2)

 let base64String = imageData!.base64EncodedStringWithOptions(.allZeros)

Once I moved to Xcode 7 and Swift 2 the following error appeared:

type of expression is ambiguous without more context

So I tried:

let base64String = imageData!.base64EncodedStringWithOptions(options: NSDataBase64EncodingOptions.allZeros)

But there is no "allZeros" option among NSDataBase64EncodingOptions.


回答1:


You should use .Encoding64CharacterLineLength instead of .allZeros:

let imageData = UIImageJPEGRepresentation(firstImageView.image!, 0.2)

let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)


来源:https://stackoverflow.com/questions/32967765/base64-encoding-decoding-with-swift-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!