gpt4 book ai didi

ios - alamofire 在 swift 3 中上传相机和图库图像

转载 作者:行者123 更新时间:2023-11-29 00:12:33 25 4
gpt4 key购买 nike

您好,我花了 20 多个小时来弄清楚如何将图像从我的应用程序上传到服务器,我想上传的图像可以是从相机或照片卷中拍摄的......这是我的代码确实显示上传进度,但未到达服务器,因此从服务器得到否定响应。请帮助我。

  Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(UIImageJPEGRepresentation(image, 0.1)!, withName: imageName)
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to: URL_USER_PROFILE_IMG_UPLOAD)
{ (result) in
switch result {
case .success(let upload, _, _):

upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

upload.responseJSON { response in
print(response.result.value)
}

case .failure(let encodingError):
print(encodingError)
}
}

我的服务器代码是这样用 PHP 编写的

    <?php

// Path to move uploaded files
$target_path = "profile-photos/";

// array for final json respone
$image_upload = array();
$server_ip ="00.000.000.000";
//gethostbyname(gethostname());
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip .'/'.'folder2017'.' /'.'webser'.'/'. $target_path;



if(isset($_FILES['image']['name'])) {
$target_path=$target_path.$_FILES['image']['name'];
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
$image_upload=array('path'=>$file_upload_url,'response_code'=>1);
echo json_encode($image_upload);
}
} else {

$image_upload=array('response_code'=>0);
echo json_encode($image_upload);
}



?>

最佳答案

试试这个:

替换

multipartFormData.append(UIImageJPEGRepresentation(image, 0.1)!, withName: imageName)

let imgData = UIImageJPEGRepresentation(image!, 1.0)!
multipartFormData.append(imgData, withName: "image", fileName: "image.jpg", mimeType: "image/jpg")

关于ios - alamofire 在 swift 3 中上传相机和图库图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46039567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com