简体   繁体   English

如何使用Alamofire内部参数上传图像

[英]How to upload an image with Alamofire inside parameters

There's a lot of similar questions on here, but i couldn't find an answer that's help me in solving this problem. 这里有很多类似的问题,但是我找不到答案可以帮助我解决这个问题。

What I want to do is upload an image with Alamofire with parameters and the image itself should be part of the parameters. 我想要做的是使用Alamofire上传带有参数的图像,并且图像本身应该是参数的一部分。 For example the parameters should be like this: 例如,参数应如下所示:

["user_id": 1, "picture": 'and here will be the image that i want to upload']

in the response i will be getting the image as a link like this: 在响应中,我将获得图像作为这样的链接:

"/uploads/members/'user_id'/images/member_'user_id'/28121284ase2.jpg"

something similar to that. 类似的东西。

Also the image should be ' jpg, png and not more than 5MB '. 图像也应为“ jpg,png和不超过5MB”。

I'm new to uploading images w\\ API so I don't what to do exactly. 我是使用API​​上传图片的新手,所以我不知道该怎么做。 I tried every solution I could find in here and google but no luck so far. 我尝试了在这里和Google都能找到的所有解决方案,但到目前为止还没有运气。 Any help would be appreciated. 任何帮助,将不胜感激。

I'm going to assume that you have an instance of UIImage to start with, you'll have to encode it to base64 and send it to your backend as you would any other String parameter. 我将假设您首先有一个UIImage实例,您必须将其编码为base64并将其发送到后端,就像处理其他任何String参数一样。 This should help : 这应该有帮助:

extension UIImage {

    func toBase64() -> String? {
        guard let imageRectoData = jpeg(.low) else {
            return nil
        }
        return imageRectoData.base64EncodedString()
    }

    enum JPEGQuality: CGFloat {
        case lowest  = 0
        case low     = 0.25
        case medium  = 0.5
        case high    = 0.75
        case highest = 1
    }

    /// Returns the data for the specified image in JPEG format.
    /// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.
    /// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
    func jpeg(_ jpegQuality: JPEGQuality) -> Data? {
        return jpegData(compressionQuality: jpegQuality.rawValue)
    }
}

You can do that by using multipart/form-data request. 您可以通过使用multipart / form-data请求来实现。 your question is relative to this one: Upload image with multipart form-data iOS in Swift 您的问题与这一问题有关: 在Swift中使用多部分表单数据iOS上传图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带参数的 Alamofire 在服务器上上传图像 - how to upload Image on server using Alamofire with parameters 带有参数的 Alamofire 多格式上传图像 - Alamofire Multiform Upload Image with Parameters 使用 alamofire 上传带参数的图像 - Upload Image with Parameters using alamofire 如何在 Swift 中使用 Alamofire 上传带有 JSON 参数的图像? - How can I upload image with JSON parameters with Alamofire in Swift? 如何使用 alamofire 和数组形式的参数上传图像 - how to upload image using alamofire and parameters in array form 如何使用alamofire上传带有其他用户详细信息作为参数的用户图像,我正在使用Alamofire(4.5.1) - How to upload a image of user with other user details as parameters using alamofire, i am using Alamofire (4.5.1) 使用其他参数上传Alamofire 4和Swift 3 Image - Alamofire 4 and Swift 3 Image upload with other parameters 如何在iOS,Swift3,Alamofire 4中使用多部分表单数据将图像作为参数上传以及其他参数 - How to upload image as a parameter, with other parameters using multipart form data in ios, swift3, Alamofire 4 使用参数和AuthToken标头上传Alamofire图像并使用补丁方法 - Alamofire Image upload with Parameters and AuthToken Header with patch method 在Swift 4中的Alamofire 4中使用multipart上传图像和多个参数 - upload image and multiple parameters using multipart in alamofire 4 in swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM