How to encode image (asset) and via photo picker to base64 in React Native (iOS)?
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
add a comment |
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
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
add a comment |
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
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
react-native base64 react-native-ios
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 at 5:44
Ron
15911
15911
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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