44

How to convert UIimage to base64 encoded string? I couldn't find any examples or codes with detailed regarding.

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
apple
  • 469
  • 1
  • 5
  • 6

7 Answers7

56

I wonder why didn't you find your question because it's a very old question & can be found here.

Anyways, You need to first add NSData categories to your project which are available from here -

header and implementation Then convert your UIImage object into NSData the following way:

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

And then apply Base64 encoding to convert it into a base64 encoded string:

NSString *encodedString = [imageData base64Encoding];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sagar
  • 3,159
  • 3
  • 29
  • 28
  • http://svn.cocoasourcecode.com/MGTwitterEngine/MGTwitterEngineGlobalHeader.h -- However it should not be required and can be removed from header – Sagar May 09 '13 at 05:48
  • anyreason that why it takes some time to covert ?? – Mr.G Jul 27 '16 at 09:44
47

There are changes in iOS 7 that allow this to be done without using any external categories to support Base64 encoding/decoding.

You could just write it directly using:

- (NSString *)base64String {
    return [UIImagePNGRepresentation(self) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 4
    Note that these methods have been introduced with iOS 7 but `base64Encoding` has been made available back to iOS 4. So unless you still support iOS 2 or 3 you can use `base64Encoding` or the one @Abizern mentions without any external frameworks. – Pascal Oct 26 '13 at 21:40
  • Thanks for mentioning the back port. – Abizern Oct 27 '13 at 09:05
  • 2
    I had better luck when passing options of `0` instead of `NSDataBase64Encoding64CharacterLineLength`. That way it does not insert any line breaks, and is usable in a `data:` URI in UIWebView. – TalkLittle Apr 01 '14 at 17:56
  • 1
    @TalkLittle try using `kNilOptions`. Same thing but more readable – Abizern Apr 01 '14 at 22:10
  • NSDataBase64EncodingEndLineWithLineFeed – Sound Blaster Jan 24 '19 at 21:57
9

You can follow below code

-(NSString *)imageToNSString:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
    return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

-(UIImage *)stringToUIImage:(NSString *)string
{
 NSData *data = [[NSData alloc]initWithBase64EncodedString:string
                                                     options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
}
Bhat
  • 602
  • 9
  • 24
7
@implementation UIImage (Extended)

- (NSString *)base64String {
    NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return [NSString stringWithUTF8String:[data bytes]];
}

@end
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
3

NSData (Base64) has changed slightly since the last reply in this thread.

you should now use:

NSData *base64EncodedImage = [UIImageJPEGRepresentation(img, 0.8) base64EncodingWithLineLength:0];
Fabio Napodano
  • 1,210
  • 1
  • 16
  • 17
2

Swift 3

I use base64EncodedString() to convert Data() object to base64 string

To convert image to base64 string

    var sample = UIImage(named: "image_logo")
    let imageData:Data =  UIImagePNGRepresentation(sample!)!
    let base64String = imageData.base64EncodedString()
dimohamdy
  • 2,917
  • 30
  • 28
0

when converting to image to base64 in ios the new line “\n” from base64 encoded strings :

use this code :


 UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];



        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex] ;

        UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];

        UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];

        tableIMAGE.image=orginalImage;



imageStris = [UIImageJPEGRepresentation(tableIMAGE.image,1)base64Encoding];

answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@",answersARRAY[indexPath.row],imageStris];

[self dismissViewControllerAnimated:YES completion:nil];