我尝试在Base64中对图像进行编码,并且我得到一个看起来正确的字符串,但是当我尝试在PHP中加载图像时,我从iOS获取了一个不正确的错误文件。 (我的Android版本的应用程序会放入可以读取的有效base64文件)
这是我的代码:
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageData = UIImagePNGRepresentation(pickedImage)
var encodedImageData = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) as NSString
let range = NSMakeRange(0, encodedImageData.length)
encodedImageData = encodedImageData.stringByReplacingOccurrencesOfString("+", withString: "%2B", options: NSStringCompareOptions.LiteralSearch, range: range)
encodedImageData = encodedImageData.stringByReplacingOccurrencesOfString("/", withString: "%2F", options: NSStringCompareOptions.LiteralSearch, range: range)
let utilisateurId = PlistManager.sharedInstance.getValueForKey("id")
我尝试了很多配置,但我无法获得一个有效的配置。
在Android尺寸上:
bitmapLoaded.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
答案 0 :(得分:1)
从相机获取图像。
Bitmap bmp1 = (Bitmap) data.getExtras().get("data");
mBitmap = Bitmap.createScaledBitmap(bmp1, 300, 300, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] imageByte = baos.toByteArray();
String encoded = Base64.encodeToString(imageByte, Base64.DEFAULT);
Log.d("tag", "base64 : " + encoded);
来自画廊,
Uri selectedImageUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
Bitmap newbmp = Bitmap.createScaledBitmap(bmp, 600, 600, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
newbmp.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] imageByte = baos.toByteArray();
String encoded = Base64.encodeToString(imageByte, Base64.DEFAULT);
已编辑的答案
我们如何在IOS中获得base64
let plainString = "foo"
let plainData = plainString.data(using: String.Encoding.utf8)
let base64String = plainData?.base64EncodedString()
print(base64String!) // Zm9v
if let decodedData = Data(base64Encoded: base64String!),
let decodedString = String(data: decodedData, encoding: String.Encoding.utf8) {
print(decodedString) // foo
}
let plainString = "foo"
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
print(base64String!) // Zm9v
let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)
print(decodedString) // foo
NSString *plainString = @"foo";
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); // Zm9v
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", decodedString); // foo
希望这会对你有所帮助。