Skip to content

Instantly share code, notes, and snippets.

@wszdwp wszdwp/Base64.swift
Last active Jul 27, 2016

Embed
What would you like to do?
Base64 en/decode in Swift
//create image instance
//with image name from bundle
var image : UIImage = UIImage(named:"imageNameHere")!
var imageData = UIImagePNGRepresentation(image)
//OR with path
var url:NSURL = NSURL(string : "urlHere")!
var imageData:NSData = NSData.dataWithContentsOfURL(url, options: nil, error: nil)
//Encoding
let base64String = imageData.base64EncodedStringWithOptions(.allZeros)
println(base64String)
//Decoding
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
var decodedimage = UIImage(data: decodedData)
println(decodedimage)
yourImageView.image = decodedimage as UIImage
@lenichols

This comment has been minimized.

Copy link

commented Mar 16, 2016

how do you use this with base64 string?

@skybirdraj

This comment has been minimized.

Copy link

commented Jul 27, 2016

whats the meaning of (.allZeros)???? can u explain me plz....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.