0

I need to convert image to base 64 string from image's uri(avatar uri) in objective c. This is my input uri

file:///Users/My/Library/Developer/CoreSimulator/Devices/81D640F9-A776-5B3D/data/Containers/Data/Application/4094435F-04-5A8A75/Documents/F67871-6762-47A5-C8CDC.jpg

This is my code,

RCT_EXPORT_METHOD(baseConvert:(NSURL *)url1)
{

  NSLog(@"My Input Uri:%@",url1);

  NSData* data = [NSData dataWithContentsOfURL:url1];

    //NSString *base64Encoded = [data base64EncodedStringWithOptions:0];


  //  NSString *base64Img = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

  NSString *base64Img = [data base64EncodedStringWithOptions:0];

  NSLog(@"My output String:%@",base64Img);

}

The above code creates a string. But it only gives upper part of image, when I check the Output String on online image decoders.Please help me ....

code hunter
  • 11
  • 1
  • 5
  • So you are saying that if you get the data, encode it the way you did and then decode them back to raw data then the size of the data in the end is not the same as in the beginning? Please ensure that you do all these tests and share results with us. – Matic Oblak Mar 16 '18 at 11:15
  • https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string – Rocky Mar 16 '18 at 11:29
  • okey.I have a url of an image. I need to convert that image into 'base 64 string'.It is my need.... – code hunter Mar 16 '18 at 11:29

1 Answers1

0

I used this code for converting URL to Base64String, its working for me. Try it this.

 - (void)viewDidLoad
 {
    [super viewDidLoad];

    [self Base64StringFromImage];
 }



-(void)Base64StringFromImage
{
  NSString *base64String;
  NSURL *Sample_ImgUrl =  [NSURL URLWithString:@"https://gigaom.com/wp- content/uploads/sites/1/2010/06/macbookair_large.png?w=604"];

  NSData *imageData = [NSData dataWithContentsOfURL:Sample_ImgUrl];


 base64String=[imageData base64EncodedStringWithOptions:0];
 NSLog(@"Image  %@", base64String);

}




After Decode this base64String to Image  :- 

enter image description here