Skip to content

Instantly share code, notes, and snippets.

@mikefogg
Created June 28, 2018 13:56
Show Gist options
  • Save mikefogg/a239a87d6c06c5e4f04d22825eaea061 to your computer and use it in GitHub Desktop.
Save mikefogg/a239a87d6c06c5e4f04d22825eaea061 to your computer and use it in GitHub Desktop.
React Native NativeModue for turning HEIC images (or any image in the asset-library directory) into JPEG base64 encoded data

Usage:

import { NativeModules } from 'react-native'

NativeModules.ImageToBase64.convertToBase64({
  assetPath: image.uri // an asset-libary url like the one you get from CameraRoll
}).then(result => {
  console.log('success!', result.base64)
})

Issues:

  1. Needs better failure catching
  2. Isn't currently maintaining orientation... that needs to get fixed. We have access to it before we create the jpeg image I'm just not sure how to use that to make sure that the jpeg conversion maintains the EXIF data from the original image.
//
// ImageToBase64.h
//
// Created by Mike Fogg on 6/28/18.
//
#import <React/RCTBridgeModule.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface ImageToBase64 : NSObject <RCTBridgeModule>
@end
//
// ImageToBase64.m
//
// Created by Mike Fogg on 6/28/18.
//
#import "ImageToBase64.h"
@implementation ImageToBase64
//export the name of the native module as 'ImageToBase64' since no explicit name is mentioned
RCT_EXPORT_MODULE();
//exports a method convert to javascript
RCT_EXPORT_METHOD(convertToBase64: (NSDictionary*) options
resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject)
{
NSString *uri = options[@"assetPath"];
// Take the asset path we get, and get the local identifier of the image
NSURLComponents *components = [NSURLComponents componentsWithString:uri];
NSArray *queryItems = components.queryItems;
NSString *assetId = [self valueForKey:@"id" fromQueryItems:queryItems];
// Go get a list of assets with this identifier (should be one?)
PHFetchResult* assetList = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:NULL];
PHAsset *imageAsset = [assetList firstObject];
// Go get the actual image data
PHImageManager *imageManager = [PHImageManager defaultManager];
[imageManager requestImageDataForAsset:imageAsset options:NULL resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
UIImage *image = [UIImage imageWithData:imageData];
resolve(@{ @"base64": [self encodeToBase64String: image] });
}];
}
- (NSString *)valueForKey:(NSString *)key fromQueryItems:(NSArray *)queryItems
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
NSURLQueryItem *queryItem = [[queryItems
filteredArrayUsingPredicate:predicate]
firstObject];
return queryItem.value;
}
-(NSString *) encodeToBase64String:(UIImage *)image {
return [UIImageJPEGRepresentation(image, 1.0f) base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment