apple
apple

Reputation: 469

UIImage to base64 String Encoding

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

Upvotes: 44

Views: 54450

Answers (7)

Sagar
Sagar

Reputation: 3149

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];

Upvotes: 56

dimohamdy
dimohamdy

Reputation: 3053

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()

Upvotes: 2

Bhat
Bhat

Reputation: 602

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];
}

Upvotes: 9

user6694170
user6694170

Reputation:

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];



Upvotes: 0

Abizern
Abizern

Reputation: 150615

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];
}

Upvotes: 47

Fabio Napodano
Fabio Napodano

Reputation: 1249

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

you should now use:

NSData *base64EncodedImage = [UIImageJPEGRepresentation(img, 0.8) base64EncodingWithLineLength:0];

Upvotes: 3

Peter Lapisu
Peter Lapisu

Reputation: 20975

@implementation UIImage (Extended)

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

@end

Upvotes: 7

Related Questions