Convert between UIImage and Base64 string
Swift
First we need to have image's NSData
//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 > Encoding
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0 > Decoding
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Swift 3.0 > Decoding
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
Encoding :
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
Decoding :
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
print(decodedimage)
yourImageView.image = decodedimage
Swift 3.0
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage
Objective-C
iOS7 > version
You can use NSData's
base64EncodedStringWithOptions
Encoding :
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
Decoding :
- (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
image
Add Base64 class in your project
.
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
Convert UIImage to base64 string in swift
Do it something like this:
For Encoding:
data.base64EncodedStringWithOptions([])
For decoding:
let url = URL(string: String(format:"data:application/octet-stream;base64,%@",base64String))
do {
let data = try Data(contentsOf: url!)
}catch {
}
Decode a base64 data to image
Instead of using NSData
use directly Swift 3 native Data
.
if let decodedData = Data(base64Encoded: mediaFile, options: .ignoreUnknownCharacters) {
let image = UIImage(data: decodedData)
}
Converting Image to BASE64 String in swift
Actually it will not take time to convert but for printing, it will take more time so don't print it...
Base64 string to UIImage Issue
Your Base64 string
%2F9j%2F4AAQSk...Z2uHi4%2BTl5ufo6...jtTGf%2FZ
contains "percent-encoded hexadecimal sequences",
such as "%2F" for "/", or "%2B" for "+".
That is a variant used in URL applications, but Data
does not handle it automatically. Therefore you have to remove the
percent encodings first:
if let percentDecoded = base64String.removingPercentEncoding,
let decodedData = Data(base64Encoded: percentDecoded) {
// ...
}
Related Topics
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections
How to Develop or Migrate Apps For Iphone 5 Screen Resolution
Ios Uiimagepickercontroller Result Image Orientation After Upload
How to Force a Uiviewcontroller to Portrait Orientation in iOS 6
Assertion Failure in Dequeuereusablecellwithidentifier:Forindexpath:
How to Create a Uicolor from a Hex String
How to Create a Multiline Textfield in Swiftui
How to Get My Ip Address Programmatically on Ios/Macos
Why It Shows "Ld: Framework Not Found Bolts"
How to Adjust Font Size to Fit Height and Width of Uilabel
How to Deal With the Nsdateformatter Locale "Feechur"
How to Enable Native Resolution For Apps on Iphone 6 and 6 Plus
How to Detect When Someone Shakes an Iphone
When Should I Compare an Optional Value to Nil