Swift以纵向模式捕获照片并上传到服务器 - 上传的图像显示方向错误

问题描述:

If I take a picture with my iPhone in portrait mode, and send this picture with alamofire to server -> the image will stored perfectly. If I try to use this picture in html, the orientation is wrong. The image is rotated -90 degrees. I tried to save the image in jpg, and png format, but I have the same issue both.

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(imageData, withName: "image", fileName: "image.png", mimeType: "image/png")
    },
        to: SERVER_URL,
        encodingCompletion: { encodingResult in
            switch encodingResult {.....

On the server side:

<?php
.....
$response['error'] = "NULL";
$filename = uniqid() . ".png";
move_uploaded_file($_FILES['image']['tmp_name'], "image/" . $filename) ...... 
 ?>

I think there will be 2 different solution, but I could not find the correct way. 1) I have to change the orientation on my iPhone device somehow 2) I have to change the orientation on the server side before I upload to the server.

(I don't want to make the solution on the client side)

You can get UIImage orientation using imageOrientation property and pass it to the server side as a parameter, and then to decide what to do on the backend.

Also you can change image orientation using the code below:

let pngImage = #imageLiteral(resourceName: "yourImageFileName")
let image = UIImage(cgImage: pngImage.cgImage, scale: pngImage.scale,   orientation: UIImage.Orientation.up)