NSString *base64String = [UIImagePNGRepresentation(myimage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
I am trying to convert uiimage to base64 string. But i think it gives wrong result. It gives this string:
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAA\\r\\nABxpRE9UAAAAAgAAAAAAAAAMAAAAKAAAAAwAAAAMAAAAnHRoHj4AAABoSURBVEgN\\r\\nYvj//z8DLTFNDQc5fNQCgiHAcJSBxekYA+MmIN5PZbwJZDbDMQam+0D8n0b4PtAC\\r\\nxnU0MhzoaMZ1QAsYhIAWnKOBJUAzGYTAkUQDS8CGo+QDKloCNxzFAhCHCpagGA4y\\r\\nEwAAAP//PscxrwAAAGxJREFU7ZJBCsAgDASX/qB+22cVPPZLafZQsGDM2rOHhZDE\\r\\nGQjCzNCnAaXhuD22GH+D0rNYf+Dv8IdkCA8FHCxIQvhUIEqm8FSQSFK4JAgkElwW\\r\\ncPECTv9VlWHNnhJpSQFFO1uQXiBdiG6r9h9C14aGNgNBwQAAAABJRU5ErkJggg==
But this is an empty image. What is wrong?
Change:
NSString *base64String = [UIImagePNGRepresentation(myimage) base64EncodedStringWithOptions:NSDataBase64DecodingIgnoreUnknownCharacters];
To:
NSString *base64String = [UIImagePNGRepresentation(myimage) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
types of encoding:
NSDataBase64Encoding64CharacterLineLength
NSDataBase64Encoding76CharacterLineLength
NSDataBase64EncodingEndLineWithCarriageReturn
NSDataBase64EncodingEndLineWithLineFeed
types of decoding:
NSDataBase64DecodingIgnoreUnknownCharacters
you are using type of decoding while encoding image
In the post data your base64 "+" character was translate to " " character.
So you have got the invalid base64 string.
UIImage *img = [UIImage imageNamed:@"white.jpeg"];
NSData *imageData = UIImagePNGRepresentation(img);
NSString *imageString = [imageData base64EncodedStringWithOptions:0]; // type one
NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//type two
NSString *str = [imageString stringByReplacingOccurrencesOfString:@"+" withString:@" "];
Hope this will help you. Any one type to achieve the encode string
Generally when we convert image into Base64 Encoding,we need to convert Decoding the image.You encoded the image only.After that you must convert into decoded format.
Encoding
UIImage *imageEncode = [UIImage imageNamed:@"yourImage.png"]; // or yourImage.jpeg
NSData *dataImageEncode = UIImageJPEGRepresentation(imageEncode, 1.0);
//OR
NSData *dataImage = UIImagePNGRepresentation(image);
NSString *strEncodedBase64 = [dataImageEncode base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Decoding
NSData *dataImageDecode = [[NSData alloc]initWithBase64EncodedString:strEncodedBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *imageDecode = [UIImage imageWithData:dataImageDecode];
I have made one method in which you just need to pass uiimage and it will return a base64 string.
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.