//
//  RNImageToBase64.m
//  nativeModules
//
//  Created by 398403854@qq.com陈伟 on 2017/3/7.
//  Copyright © 2017年 drwine. All rights reserved.
//

#import "RNImageToBase64.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <UIKit/UIKit.h>

@implementation RNImageToBase64

RCT_EXPORT_MODULE(DRImageToBase64);

RCT_EXPORT_METHOD(getBase64String:(NSString *)input callback:(RCTResponseSenderBlock)callback)
{
    
    if (input.length >0) {
        @autoreleasepool {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSURL *url=[NSURL URLWithString:[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                NSData *imageData=[NSData dataWithContentsOfURL:url];
                if (imageData) {
                    NSString *base64Encoded = [@"data:image/jpg;base64,"stringByAppendingString:[imageData base64EncodedStringWithOptions:0]];
                    callback(@[[NSNull null], base64Encoded]);
                }else{
                    callback(@[@"error"]);
                }
                
            });
        }
    }
}


@end
