如何在不丢失exif数据的情况下将UIImage转换为JPEG?

问题描述 投票:0回答:1

我目前正在使用iOS应用程序,并且正在使用分段图像上传将图像上传到服务器。以下是我的图片上传方法。

func uploadImageData(imageType:Int, uploadId:String, fileName:String, imageFile:UIImage, completion:@escaping (APIResponseStatus, ImageUploadResponse?) -> Void) {

        let image = imageFile
        let imgData = image.jpegData(compressionQuality: 0.2)!

        let params = [APIRequestKeys.imageType:imageType, APIRequestKeys.uploadId:uploadId, APIRequestKeys.fileName:fileName] as [String : Any]
        //withName is the post request key for the image file
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imgData, withName: APIRequestKeys.imageFile, fileName: "\(fileName).jpg", mimeType: "image/jpg")
                for (key, value) in params {
                    multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
                }
        }, to: Constants.baseUrl + APIRequestMetod.uploadImageData, headers:self.getImageUploadHeaders())
            { (result) in
                switch result {
                case .success(let upload, _, _):
                    APIClient.currentRequest = upload
                    upload.uploadProgress(closure: { (progress) in
                    })
                    upload.responseObject {
                        (response:DataResponse<ImageUploadResponse>) in
                        switch response.result {
                        case .success(_):
                            completion(APIClient.APIResponseStatus(rawValue: (response.response?.statusCode)!)!, response.value!)
                        case .failure(let encodingError):
                            if let err = encodingError as? URLError, err.code == .notConnectedToInternet {
                                completion(APIClient.APIResponseStatus.NoNetwork, nil)
                            } else {
                                completion(APIClient.APIResponseStatus.Other, nil)
                            }
                        }
                    }
                case .failure( _):
                    completion(APIClient.APIResponseStatus.Other, nil)
                }
            }
        }

但是对于此实现,服务器始终发送exif数据错误。以下是我遇到的错误。

exif_read_data(A029C715-99E4-44BE-8691-AA4009C1F5BD_FOTOPREGUNTA.ico): Illegal IFD size in
upload_image_xhr.php on line

重要的是,这项服务在POSTMAN和android应用程序中也都正常运行。此错误仅适用于我的iOS实现。我的后端开发人员告诉我,我发送的数据中存在exif数据错误,请从我这一边验证数据。有人对此有想法吗?预先感谢。

ios swift alamofire multipartform-data
1个回答
0
投票

我将创建块功能以使用多部分将图像上传到服务器

//Here strUrl = YOUR WEBSERVICE URL
//postParam = post Request parameter i.e. 
//let postParam : [String : Any] = [first_name : "name"]
//imageArray = image upload array i.e.
//var imageArray : [[String:Data]] = [["image_name" : YOUR IMAGE DATA]]

func postImageRequestWithURL(withUrl strURL: String,withParam postParam: Dictionary<String, Any>,withImages imageArray:[[String:Data]], completion:@escaping (_ isSuccess: Bool, _ response:NSDictionary) -> Void)
{
    let requetURL = strURL

    Alamofire.upload(multipartFormData: { (MultipartFormData) in

        for (imageDic) in imageArray
        {
            for (key,valus) in imageDic
            {
                MultipartFormData.append(valus, withName:key,fileName: "file.jpg", mimeType: "image/jpg")
            }
        }

        for (key, value) in postParam
        {
            MultipartFormData.append("\(value)".data(using: .utf8)!, withName: key)

          // MultipartFormData.append(value, withName: key)
        }

    }, usingThreshold: UInt64.init(), to: requetURL, method: .post, headers: ["Accept": "application/json"]) { (result) in

        switch result {
        case .success(let upload, _, _):

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

            upload.responseJSON { response in

                let desiredString = NSString(data: response.data!, encoding: String.Encoding.utf8.rawValue)

                print("Response ====================")

                print(desiredString!)

                if let json = response.result.value as? NSDictionary
                {
                    if response.response?.statusCode == 200
                        || response.response?.statusCode == 201
                        || response.response?.statusCode == 202
                    {
                        completion(true,json);
                    }
                    else
                    {
                        completion(false,json);
                    }
                }
                else
                {
                    completion(false,[:]);
                }
            }

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

            completion(false,[:]);
        }

    }
}

我希望这会帮助...

© www.soinside.com 2019 - 2024. All rights reserved.