Skip to content

Instantly share code, notes, and snippets.

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

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)
let base64String = imageData.base64EncodedStringWithOptions(.allZeros)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
var decodedimage = UIImage(data: decodedData)
yourImageView.image = decodedimage as UIImage

This comment has been minimized.

Copy link

commented Mar 16, 2016

how do you use this with base64 string?


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.