0

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. 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 '.

I'm new to uploading images w\ API so I don't what to do exactly. I tried every solution I could find in here and google but no luck so far. Any help would be appreciated.

Marwen Doukh
  • 1,946
  • 17
  • 26
Yasser
  • 265
  • 1
  • 8
  • The problem before Alamofire is the backend where you would like to upload images. What parameters does your APIs expect? What kind of call you should do? To know how to upload with Alamofire, you must have this information before. – Alessandro May 23 '19 at 12:11
  • the parameters for the API are 10 including the image. ' name(string), cpr(int), gender(int: 1 or 2), phone(string), email(string), weight(int), height(int), password(string), device_id(string) and the picture (png, jpg). the call will be POST. I want to upload the image into the server website. the image will be available to the user in the website and in the app. I don't have a lot of information about the website because i'm only working w\ the ios. i hope this information enough. – Yasser May 23 '19 at 12:17
  • You also need to know which content-type and format (base64 for example) your server expects to receive the image. – Alessandro May 23 '19 at 12:37

2 Answers2

1

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. 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)
    }
}
iMahdi
  • 338
  • 2
  • 9
  • thank you for your answer. I tried this but i keep getting an internal error 'message = "Undefined index: picture"; name = "PHP Notice"; i used the extension to upload the image and that's what i keep getting. – Yasser May 23 '19 at 13:13
  • You'll have to confirm with your backend developer, the fact that he's expecting the image as a base64 encoded String. – iMahdi May 23 '19 at 13:36
0

You can do that by using multipart/form-data request. your question is relative to this one: Upload image with multipart form-data iOS in Swift

Abdo
  • 342
  • 1
  • 8