Decode Base-64 encoded PNG in an NSString
NSData Base64 library files will help you.
#import "NSData+Base64.h"
//Data from your string is decoded & converted to UIImage
UIImage *image = [UIImage imageWithData:[NSData
dataFromBase64String:strData]];
Hope it helps
Swift 3 Version
It's pretty much the same
//Create your NSData object
let data = NSData(base64Encoded: "yourStringData", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
//And then just create a new image based on the data object
let image = UIImage(data: data as! Data)
Swift 2.3 Version
//Create your NSData object
let data = NSData(base64EncodedString: "yourStringData", options: .IgnoreUnknownCharacters)
//And then just create a new image based on the data object
let image = UIImage(data: data!)
Decode Base-64 encoded PNG in an NSString
NSData Base64 library files will help you.
#import "NSData+Base64.h"
//Data from your string is decoded & converted to UIImage
UIImage *image = [UIImage imageWithData:[NSData
dataFromBase64String:strData]];
Hope it helps
Swift 3 Version
It's pretty much the same
//Create your NSData object
let data = NSData(base64Encoded: "yourStringData", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
//And then just create a new image based on the data object
let image = UIImage(data: data as! Data)
Swift 2.3 Version
//Create your NSData object
let data = NSData(base64EncodedString: "yourStringData", options: .IgnoreUnknownCharacters)
//And then just create a new image based on the data object
let image = UIImage(data: data!)
Base64 String when decoded to png is cut off with Objective-C
In terms of why it's getting cut off, I suspect you're looking at the base64 string in the debugger, which will truncate it. Actually NSLog
the string and you'll see it's longer than what you're seeing in the debugger.
A couple of other unrelated observations:
You should not use
stringWithUTF8String
with[base64 bytes]
because theNSData
will not be null terminated. If you really needed to convert it to a string, you'd useinitWithData
rather thanstringWithUTF8String
:return [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
As others have pointed out, you can bypass the creation of the
NSData
of thebase64
altogether, and create the string directly:return [imageData base64EncodedStringWithOptions:0];
I'm not sure why you're taking the
NSData
from the server and round tripping it through aUIImage
at all. You can theoretically just encode the data from the server directly:NSURL* imageUrl = [NSURL URLWithString:url];
NSData* urlData = [NSData dataWithContentsOfURL:imageUrl];
return [urlData base64EncodedStringWithOptions:0];The server is already returning you the
NSData
of a PNG representation. You don't need to do thatUIImage
andUIImagePNGRepresentation
stuff at all. You're actually generating aPNG
that is considerably larger than the one the server returned to you.I'd advise against using
dataWithContentsOfURL
, because that's a synchronous network call. You probably should useNSURLSession
and change this to be an asynchronous method.
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
How to let local .png image convert to base64 string
Use this code:
NSData *imageData = UIImagePNGRepresentation(image);
NSString * base64String = [imageData base64EncodedStringWithOptions:0];
Hope this helps.
Base 64 decoding produces empty
Avoid converting to a string before decoding:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
Some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: NSDataBase64DecodingIgnoreUnknownCharacters
.
Base64 Encoding for NSString
How do I do base64 encoding on iphone-sdk?
Have a look at this.
and then use these methods to make use of the above methods(given in link)
Converting NSString to Base64 Data for XML Serialization
taken from above link.
+ (NSString *)toBase64String:(NSString *)string {
NSData *data = [string dataUsingEncoding: NSUnicodeStringEncoding];
NSString *ret = [NSStringUtil base64StringFromData:data length:[data length]];
return ret;
}
+ (NSString *)fromBase64String:(NSString *)string {
NSData *base64Data = [NSStringUtil base64DataFromString:string];
NSString* decryptedStr = [[NSString alloc] initWithData:base64Data encoding:NSUnicodeStringEncoding];
return [decryptedStr autorelease];
}
Convert array of image path into base64 encoded string.?
Try this to cycle all your images and save each encoded string into a new array:
NSMutableArray *encodedImages = [NSMutableArray new];
for (NSString *path in recipeImages)
{
UIImage *image = [UIImage imageWithContentsOfFile:path];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *dataString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
[encodedImages addObject:dataString];
}
Related Topics
Wkwebview Page Height Issue on iPhone X
Xcode Process Launch Failed: Security
Disable Autolayout Constraint Error Messages in Debug Console Output in Xcode
What Are the Benefits of Using Storyboards Instead of Xib Files in iOS Programming
Nsinteger and Nsuinteger in a Mixed 64Bit/32Bit Environment
Enable or Disable iPhone Push Notifications Inside the App
How to Use Navigation Controller Inside of Uitabbarcontroller with Storyboard on Swift
Xcode 5 Crashes -- Xcode Quit Unexpectedly
Is It Possible For Uistackview to Scroll
Swift - Apply Local CSS to Web View
Specifying One Dimension of Cells in Uicollectionview Using Auto Layout
Notificationcenter Issue on Swift 3
Add Shadow on Uiview Using Swift 3
Uisearchbar: Clear Background Color or Set Background Image
How to Do Programmatically Gradient Border Color Uibutton with Swift
How to Change the Default Text of Cancel Button Which Appears in the Uisearchbar +Iphone