1

I tried the solution given here using:

- (NSString *)encodeToBase64String:(UIImage *)image {
    return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

The encoded string I am getting is different from the one I can get via uploading an image on web tool like.

Simply my string is different from web and is not able to decode on the web for an image.

my other implementation is

-(NSString*)base64StringForImage{
    UIImage *originalImage =_image_PreviewImage.image;
    UIGraphicsBeginImageContext(originalImage.size);
    [originalImage drawInRect:CGRectMake(0, 0, 1000, 1000)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        NSData *imgData=UIImagePNGRepresentation(newImage);
NSString *base64String = [imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    return base64String;
}

originalImage and newImage both have the images. testing out reducing the image sizes

the option with

NSDataBase64EncodingEndLineWithLineFeed

given encoded string with half image like the below

enter image description here

Shekhu
  • 1,998
  • 5
  • 23
  • 49
  • Perhaps you need to provide different options. Show an example of the base64 encoding you actually want (not the whole string, just the first 100 and last 100 characters). Do the same for the actual output you are getting. – rmaddy Apr 30 '19 at 16:17
  • I tried with NSDataBase64Encoding64CharacterLineLength , NSDataBase64EncodingEndLineWithCarriageReturn , NSDataBase64EncodingEndLineWithLineFeed the string is still different as decoding didn't work on the web using the string returned – Shekhu Apr 30 '19 at 16:19
  • Did you try using UIImagePNGRepresentation instead of UIImageJPEGRepresentation, those might end up with different character strings after base64 encoding... – Michael Dougan Apr 30 '19 at 16:32
  • yes tried same concern as the resultant string upon decoding not give the image on web @MichaelDougan – Shekhu Apr 30 '19 at 16:52
  • I have done this task but it is Swift. You can convert it from any good site like https://objectivec2swift.com/#/converter/code/ My code: let logo = UIImage(named: "test.png")! let imageData:Data = logo.pngData()! let base64String = imageData.base64EncodedString() print("\n\n\n\n *** BASE64 STRING: ",base64String," \n\n\n\n\n") Tested, working fine in Swift 5 – Ravi Apr 30 '19 at 17:12

0 Answers0