Last active
July 27, 2016 05:59
-
-
Save wszdwp/4427d3678bcbda97840f to your computer and use it in GitHub Desktop.
Base64 en/decode in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
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
how do you use this with base64 string?