encode image to base64, get a invalid base64 string (ios using base64EncodedStringWithOptions)

25,269

Solution 1

I think you should replace the your options parameter

Change:

NSString *imageString = [imageData base64EncodedStringWithOptions:0];

To:

NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

And just in case you'd like it:

- (NSString *)imageToNSString:(UIImage *)image
{
    NSData *data = UIImagePNGRepresentation(image);

    return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

- (UIImage *)stringToUIImage:(NSString *)string
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];

    return [UIImage imageWithData:data];
}

Remember that this is an iOS 7 api.

Solution 2

replace + to %2B ios replace all + to " " space that make image invalid

also backend must handle it too

- (NSString *)base64String:(UIImage*)image {return [[UIImageJPEGRepresentation(image,1) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];}

Solution 3

Yap, I find the problem is I don't encode the url.

In the post data my base64 "+" character was translate to " " character.

So I get the invalid base64 string.

Thanks Logan.

Solution 4

inspired from Fadi Abuzant answer here is the Swift 3 version

stringBase64 = stringBase64.replacingOccurrences(of: "+", with: "%2B")
Share:
25,269
Yu-Shing Shen
Author by

Yu-Shing Shen

Updated on May 04, 2020

Comments

  • Yu-Shing Shen
    Yu-Shing Shen about 4 years

    Here is my code.

      UIImage *img = [UIImage imageNamed:@"white.jpeg"];
      NSData *imageData = UIImagePNGRepresentation(img);
      NSString *imageString = [imageData base64EncodedStringWithOptions:0];
      NSLog(@"%@", imageString);
    

    And I always get invalid base64 string which include space.

    iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAIAAAC MS2jAAAAHGlET1QAAAACAAAAAAAAAFwAAAAoAAAAXAAAAFsAAAMC oRdmgAAAs5JREFUeAHs1tFKI1EABFH//5NNVh02uA LQhwoCZ0zj5LbTlffInl58yCAwM8JvPz8iBMIIPDGHJcA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oBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCyilqNgSU4wcIFAHlFDUbAsrxAwSKgHKKmg0B5fgBAkVAOUXNhoBy/ACBIqCcomZDQDl gEARUE5RsyGgHD9AoAgop6jZEFCOHyBQBJRT1GwIKMcPECgCB/GnRcSfso/fAAAAAElFTkSuQmCC
    

    So Confused.

    When I try remove the space in base64 string, and decode it.

    I can't get origin image.

  • Yu-Shing Shen
    Yu-Shing Shen about 10 years
    I have tried your advise. But I still got invalid base64 string
  • Logan
    Logan about 10 years
    Hi Yu, I have been using this for a while, I think your error is somewhere else. Did you try using the methods?
  • Crashalot
    Crashalot about 8 years
    Does it make a difference generally if you use EncodingEndLineWithLineFeed instead of Encoding64CharacterLineLength (Swift)? Encoding UIImage to base64 string isn't quite working, but will trying using EncodingEndLineWithLineFeed
  • Logan
    Logan about 8 years
    @Crashalot I don't know enough about base64 to give you strong information on this subject, all I can say is when I needed this functionality, the above code worked for me. I think line feed means each line is broken up by \n and the other uses a different indicator for line lengths. Not sure the pros and cons of one over the other.
  • Crashalot
    Crashalot about 8 years
    ok thanks logan. did you happen to encounter this same problem: stackoverflow.com/questions/37034221/…
  • Logan
    Logan about 8 years
    @Crashalot at the time, no. We ran the server, so the strings were formatted in a way we could work with. It might be possible they use some special formatting, but I haven't dealt w/ this stuff in almost 2 years, so it's not very fresh in my mind. Sorry I can't be more help