How to encode image (asset) and via photo picker to base64 in React Native (iOS)?









up vote
0
down vote

favorite












I've been struggling to find a supported solution to encode images (from assets) and photo picker to base64string.



I can do this via Swift in a straight native app.



func convertImageTobase64(format: ImageFormat, image:UIImage) -> String? 
var imageData: Data?
switch format
case .png: imageData = image.pngData()
case .jpeg(let compression): imageData = image.jpegData(compressionQuality: compression)

return imageData?.base64EncodedString()




var mylogo: UIImage? = UIImage.init(named: "DFU-180x180")
let base64String = convertImageTobase64(format: .png, image: mylogo!)
let dataString = "data:image/jpg;base64," + base64String!


I tried to do this via NativeModules, but I get errors for RCTConvert being run on background thread instead of main.
Images.h



#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>


@interface Images : NSObject <RCTBridgeModule>
@end


Images.m



#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Images.h"

@implementation Images
RCT_EXPORT_MODULE()

// All the methods are implemented in a Swift extension, see FileBridgeExtension.swift
RCT_EXTERN_METHOD(convertImageTobase64:(nonnull NSString*)format image:(nonnull UIImage*)image callback:(RCTResponseSenderBlock))

@end


ImagesExtension.swift



import UIKit

public enum ImageFormat
case png
case jpeg(CGFloat)


@objc extension Images

@objc func convertImageTobase64(_ format: NSString, image:UIImage, callback: @escaping ([Any]?)->Void)
var imageData: Data?
print("convertImageTobase64_line 1")
print("convert format: " + (format as! String))
switch format
case ".png": imageData = UIImagePNGRepresentation(image)
print("convertImageTobase64_line 2")
case ".jpeg": imageData = UIImageJPEGRepresentation(image, 1.0)
print("convertImageTobase64_line 3")
default:
print("convertImageTobase64_line 4")
let error = RCTMakeError("Invalid image format", nil, nil)
callback([[error], ]);

let base64string = imageData?.base64EncodedString()
print("convertImageTobase64_line 5 = " + base64string!)
callback([[NSNull()], [base64string]]);





I've tried 4 different React Native libraries and nothing works. I get errors that the library doesn't exist, even thought I do the npm install and confirm the library exists in node_modules. I even remove the node_modules folder, and rebuild it with npm install.



2 of the libraries that I've tried.



npm version that I'm using is: 6.4.1
node version that I'm using is: 8.12.0
Xcode v10
react-native-image-base64



react-native-image-to-base64










share|improve this question





















  • Just to be sure, did you link the libraries ?
    – Pierre Capo
    Nov 11 at 9:17










  • Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
    – Larry B
    Nov 12 at 7:21















up vote
0
down vote

favorite












I've been struggling to find a supported solution to encode images (from assets) and photo picker to base64string.



I can do this via Swift in a straight native app.



func convertImageTobase64(format: ImageFormat, image:UIImage) -> String? 
var imageData: Data?
switch format
case .png: imageData = image.pngData()
case .jpeg(let compression): imageData = image.jpegData(compressionQuality: compression)

return imageData?.base64EncodedString()




var mylogo: UIImage? = UIImage.init(named: "DFU-180x180")
let base64String = convertImageTobase64(format: .png, image: mylogo!)
let dataString = "data:image/jpg;base64," + base64String!


I tried to do this via NativeModules, but I get errors for RCTConvert being run on background thread instead of main.
Images.h



#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>


@interface Images : NSObject <RCTBridgeModule>
@end


Images.m



#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Images.h"

@implementation Images
RCT_EXPORT_MODULE()

// All the methods are implemented in a Swift extension, see FileBridgeExtension.swift
RCT_EXTERN_METHOD(convertImageTobase64:(nonnull NSString*)format image:(nonnull UIImage*)image callback:(RCTResponseSenderBlock))

@end


ImagesExtension.swift



import UIKit

public enum ImageFormat
case png
case jpeg(CGFloat)


@objc extension Images

@objc func convertImageTobase64(_ format: NSString, image:UIImage, callback: @escaping ([Any]?)->Void)
var imageData: Data?
print("convertImageTobase64_line 1")
print("convert format: " + (format as! String))
switch format
case ".png": imageData = UIImagePNGRepresentation(image)
print("convertImageTobase64_line 2")
case ".jpeg": imageData = UIImageJPEGRepresentation(image, 1.0)
print("convertImageTobase64_line 3")
default:
print("convertImageTobase64_line 4")
let error = RCTMakeError("Invalid image format", nil, nil)
callback([[error], ]);

let base64string = imageData?.base64EncodedString()
print("convertImageTobase64_line 5 = " + base64string!)
callback([[NSNull()], [base64string]]);





I've tried 4 different React Native libraries and nothing works. I get errors that the library doesn't exist, even thought I do the npm install and confirm the library exists in node_modules. I even remove the node_modules folder, and rebuild it with npm install.



2 of the libraries that I've tried.



npm version that I'm using is: 6.4.1
node version that I'm using is: 8.12.0
Xcode v10
react-native-image-base64



react-native-image-to-base64










share|improve this question





















  • Just to be sure, did you link the libraries ?
    – Pierre Capo
    Nov 11 at 9:17










  • Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
    – Larry B
    Nov 12 at 7:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've been struggling to find a supported solution to encode images (from assets) and photo picker to base64string.



I can do this via Swift in a straight native app.



func convertImageTobase64(format: ImageFormat, image:UIImage) -> String? 
var imageData: Data?
switch format
case .png: imageData = image.pngData()
case .jpeg(let compression): imageData = image.jpegData(compressionQuality: compression)

return imageData?.base64EncodedString()




var mylogo: UIImage? = UIImage.init(named: "DFU-180x180")
let base64String = convertImageTobase64(format: .png, image: mylogo!)
let dataString = "data:image/jpg;base64," + base64String!


I tried to do this via NativeModules, but I get errors for RCTConvert being run on background thread instead of main.
Images.h



#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>


@interface Images : NSObject <RCTBridgeModule>
@end


Images.m



#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Images.h"

@implementation Images
RCT_EXPORT_MODULE()

// All the methods are implemented in a Swift extension, see FileBridgeExtension.swift
RCT_EXTERN_METHOD(convertImageTobase64:(nonnull NSString*)format image:(nonnull UIImage*)image callback:(RCTResponseSenderBlock))

@end


ImagesExtension.swift



import UIKit

public enum ImageFormat
case png
case jpeg(CGFloat)


@objc extension Images

@objc func convertImageTobase64(_ format: NSString, image:UIImage, callback: @escaping ([Any]?)->Void)
var imageData: Data?
print("convertImageTobase64_line 1")
print("convert format: " + (format as! String))
switch format
case ".png": imageData = UIImagePNGRepresentation(image)
print("convertImageTobase64_line 2")
case ".jpeg": imageData = UIImageJPEGRepresentation(image, 1.0)
print("convertImageTobase64_line 3")
default:
print("convertImageTobase64_line 4")
let error = RCTMakeError("Invalid image format", nil, nil)
callback([[error], ]);

let base64string = imageData?.base64EncodedString()
print("convertImageTobase64_line 5 = " + base64string!)
callback([[NSNull()], [base64string]]);





I've tried 4 different React Native libraries and nothing works. I get errors that the library doesn't exist, even thought I do the npm install and confirm the library exists in node_modules. I even remove the node_modules folder, and rebuild it with npm install.



2 of the libraries that I've tried.



npm version that I'm using is: 6.4.1
node version that I'm using is: 8.12.0
Xcode v10
react-native-image-base64



react-native-image-to-base64










share|improve this question













I've been struggling to find a supported solution to encode images (from assets) and photo picker to base64string.



I can do this via Swift in a straight native app.



func convertImageTobase64(format: ImageFormat, image:UIImage) -> String? 
var imageData: Data?
switch format
case .png: imageData = image.pngData()
case .jpeg(let compression): imageData = image.jpegData(compressionQuality: compression)

return imageData?.base64EncodedString()




var mylogo: UIImage? = UIImage.init(named: "DFU-180x180")
let base64String = convertImageTobase64(format: .png, image: mylogo!)
let dataString = "data:image/jpg;base64," + base64String!


I tried to do this via NativeModules, but I get errors for RCTConvert being run on background thread instead of main.
Images.h



#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>


@interface Images : NSObject <RCTBridgeModule>
@end


Images.m



#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Images.h"

@implementation Images
RCT_EXPORT_MODULE()

// All the methods are implemented in a Swift extension, see FileBridgeExtension.swift
RCT_EXTERN_METHOD(convertImageTobase64:(nonnull NSString*)format image:(nonnull UIImage*)image callback:(RCTResponseSenderBlock))

@end


ImagesExtension.swift



import UIKit

public enum ImageFormat
case png
case jpeg(CGFloat)


@objc extension Images

@objc func convertImageTobase64(_ format: NSString, image:UIImage, callback: @escaping ([Any]?)->Void)
var imageData: Data?
print("convertImageTobase64_line 1")
print("convert format: " + (format as! String))
switch format
case ".png": imageData = UIImagePNGRepresentation(image)
print("convertImageTobase64_line 2")
case ".jpeg": imageData = UIImageJPEGRepresentation(image, 1.0)
print("convertImageTobase64_line 3")
default:
print("convertImageTobase64_line 4")
let error = RCTMakeError("Invalid image format", nil, nil)
callback([[error], ]);

let base64string = imageData?.base64EncodedString()
print("convertImageTobase64_line 5 = " + base64string!)
callback([[NSNull()], [base64string]]);





I've tried 4 different React Native libraries and nothing works. I get errors that the library doesn't exist, even thought I do the npm install and confirm the library exists in node_modules. I even remove the node_modules folder, and rebuild it with npm install.



2 of the libraries that I've tried.



npm version that I'm using is: 6.4.1
node version that I'm using is: 8.12.0
Xcode v10
react-native-image-base64



react-native-image-to-base64







react-native base64 react-native-ios






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 3:00









Larry B

13810




13810











  • Just to be sure, did you link the libraries ?
    – Pierre Capo
    Nov 11 at 9:17










  • Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
    – Larry B
    Nov 12 at 7:21

















  • Just to be sure, did you link the libraries ?
    – Pierre Capo
    Nov 11 at 9:17










  • Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
    – Larry B
    Nov 12 at 7:21
















Just to be sure, did you link the libraries ?
– Pierre Capo
Nov 11 at 9:17




Just to be sure, did you link the libraries ?
– Pierre Capo
Nov 11 at 9:17












Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
– Larry B
Nov 12 at 7:21





Thanks @PierreCapo. I unlinked and uninstalled, and repeated the steps with a new library (npmjs.com/package/rn-img-to-base64) and had a much better experience. I get base64 data now.
– Larry B
Nov 12 at 7:21













1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can use this package rn-fetch-blob, it has this function File Stream which will serve your purpose.



Hope this helps you.



If you want further assistance, do ping me by commenting this post.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245478%2fhow-to-encode-image-asset-and-via-photo-picker-to-base64-in-react-native-ios%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can use this package rn-fetch-blob, it has this function File Stream which will serve your purpose.



    Hope this helps you.



    If you want further assistance, do ping me by commenting this post.






    share|improve this answer
























      up vote
      0
      down vote













      You can use this package rn-fetch-blob, it has this function File Stream which will serve your purpose.



      Hope this helps you.



      If you want further assistance, do ping me by commenting this post.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        You can use this package rn-fetch-blob, it has this function File Stream which will serve your purpose.



        Hope this helps you.



        If you want further assistance, do ping me by commenting this post.






        share|improve this answer












        You can use this package rn-fetch-blob, it has this function File Stream which will serve your purpose.



        Hope this helps you.



        If you want further assistance, do ping me by commenting this post.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 5:44









        Ron

        638




        638



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245478%2fhow-to-encode-image-asset-and-via-photo-picker-to-base64-in-react-native-ios%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党