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
react-native base64 react-native-ios
add a comment |
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
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 |
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
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
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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
637
637
add a comment |
add a comment |
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