Base64 Encoding / Decoding with Swift 2

My code worked well on Xcode 6.4 with Swift 1.2:

var imageData = UIImageJPEGRepresentation(firstImageView.image!, 0.2) let base64String = imageData!.base64EncodedStringWithOptions(.allZeros) 

As soon as I switched to Xcode 7 and Swift 2, the following error appeared:

expression type is ambiguous without additional context

So I tried:

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

But NSDataBase64EncodingOptions does not have the "allZeros" option.

+6
source share
1 answer

You should use .Encoding64CharacterLineLength instead of .allZeros :

 let imageData = UIImageJPEGRepresentation(firstImageView.image!, 0.2) let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) 
+9
source

All Articles