IOS Swift UIImage中的base64字符串到PHP


IOS Swift UIImage in base64 String to PHP

我正在用Swift为Iphone/Ipad开发一个应用程序。它是对安卓应用程序的改编。问题是:我调整了UIImage的大小。我想将其转换为字符串,以便在PHP服务器上发送请求。我已经在安卓

public static String BitMapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String temp = Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}

返回的字符串被读取并作为图像存储在PHP脚本的服务器中:

$lienphoto = "adressesurleserveur".$nomimage;
$binary=base64_decode($image);
$fichier = fopen($lienphoto, 'wb');
fwrite($fichier, $binary);
fclose($fichier);

我想对swift做同样的事情,我已经尝试过了:

var imageData = UIImageJPEGRepresentation(imageChoisie, 1.0)
let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

base64String对应于PHP接收的String。这不起作用,创建的文件已"损坏"。我尝试了几个小时的其他方法,但我还没有解决方案。我没有发现任何论坛帖子对此问题作出回应。

提前感谢您给我的时间。

我找到了一个解决方案。

修改是在php脚本中进行的:

 $lienphoto = "adresse sur serveur".$nomimage;
$data = str_replace('data:image/jpg;base64,', '', $image);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);
$success = file_put_contents($lienphoto, $data);

这种方法是在网站上提出的:

http://www.tricksofit.com/2014/10/save-base64-encoded-image-to-file-using-php

这保留了Swift和Android代码。我只需要创建两个php脚本来保存图像。