2

How to convert Image to base64String.

  UIImage * someImage = [UIImage imageNamed: @"page4.png"];
    NSData *imageData = UIImageJPEGRepresentation(someImage, 1.0);
//    NSString *encodedString = [imageData base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
      NSString *base64 = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding] ;
    if (base64){
       NSLog(@"Base 64 %@",base64);
      }

I the above code gives me empty value for base64 string.

@All Thanks in Advance.

3 Answers 3

5

The encoding is not correct..

Try this:

NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Sign up to request clarification or add additional context in comments.

Comments

1

Issue

You are getting empty data because you have a .png format of image trying to get data with UIImageJPEGRepresentation

Solution

You need to use UIImagePNGRepresentation

From Apple Developer:

NSData *imageData = UIImagePNGRepresentation (someImage);

P.S UIImagePNGRepresentation has no compression factor, it has just the image parameter

Comments

1
//For Encoding
UIImage * someImage = [UIImage imageNamed: @"page4.png"];
NSData *imageData=UIImagePNGRepresentation(someImage);
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

// For Decoding
NSData *data=[[NSData alloc]initWithBase64EncodedString:base64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *image = [UIImage imageWithData:data];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.