Convert UIImage to Base64 and vice versa

Code used to convert an image to base64 or an UIImage to base64 NSString




- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
    /*
     CONVERT BASE64 TO IMAGE
     */

    //Base64 image URL as string
    NSString *base64Url = @"https://dl.dropbox.com/u/37581115/base64image";

    //convert base64 url string to NSURL object
    NSURL *url = [NSURL URLWithString:base64Url];

    //fetch data from url in case if image is in utf8 format
    NSString *base64Image = [NSString stringWithContentsOfURL:url];
    
    //convert base64image to NSURL object
    url = [NSURL URLWithString:base64Image];
    
    //convert it to NSData object
    NSData *rawImageData = [NSData dataWithContentsOfURL:url];

    /*
    //SAVE TO DISK
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; // identity the home directory and file name
    [rawImageData writeToFile:jpgPath atomically:YES];
    */

    /*
     CHECK DATA TYPE OF IMAGE FILE FROM NSData
     NSString * imageExt = [self contentTypeForImageData:rawImageData];
     */

    //convert NSData to UIImage object
    UIImage *image = [UIImage imageWithData:rawImageData];

    //Set image to uiview
    imageView.image = image;

    /*
     CONVERT IMAGE TO BASE64
     */

    //Reconvert UIImage to NSData
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

    //And then apply Base64 encoding to convert it into a base-64 encoded string:
    NSString *encodedString = [self base64forData:imageData];

    //NSLog(@"encodedString is %@",encodedString);
    //Show in UITextView
    textView.text = encodedString;

}

- (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    
    switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
    }
    return nil;
}

- (NSString*)base64forData:(NSData*)theData {
    
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
    
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
    
    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;
            
            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

1 comment: