Swift
First we need to have image's NSDataphp
//Use image name from bundle to create NSData let image : UIImage = UIImage(named:"imageNameHere")! //Now use image to create into NSData format let imageData:NSData = UIImagePNGRepresentation(image)! //OR next possibility //Use image's path to create NSData let url:NSURL = NSURL(string : "urlHere")! //Now use image to create into NSData format let imageData:NSData = NSData.init(contentsOfURL: url)!
Swift 2.0 > Encodinggit
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0 > Decodinggithub
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Encoding :iphone
let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros) print(strBase64)
Decoding :post
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))! let decodedimage:UIImage = UIImage(data: dataDecoded)! print(decodedimage) yourImageView.image = decodedimage
Objective-C
iOS7 > version
You can use NSData's
base64EncodedStringWithOptions
ui
Encoding :this
- (NSString *)encodeToBase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; }
Decoding :url
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData { NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters]; return [UIImage imageWithData:data]; }
iOS 6.1 and < version
First Option : Use this link to encode
and decode
imagespa
Add Base64 class in your project
.code
Encoding :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f); NSString *strEncoded = [Base64 encode:data];
Decoding :
NSData* data = [Base64 decode:strEncoded ];; image.image = [UIImage imageWithData:data];
Another Option: Use QSUtilities for encoding
and decoding