Base64 Encoding/Decoding with Swift 2

2019-03-20 19:20发布

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条回答
叛逆
2楼-- · 2019-03-20 19:21

You should use .Encoding64CharacterLineLength instead of .allZeros:

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

let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
查看更多
登录 后发表回答