upload image with swift

Discussion in 'iOS Programming' started by erdinc27, Aug 3, 2016.

  1. erdinc27, Aug 3, 2016
    Last edited by a moderator: Aug 3, 2016

    erdinc27 macrumors regular

    Jul 20, 2011
    i have two NSDictionary and i want to upload them to a server. I searched many examples but couldn't understand well. Is there anyone who has code or any good explained tutorial. here how my code looks
    let dictInfo :NSMutableDictionary = NSMutableDictionary(objects: [MyConstants.theKey, MyConstants.theCommand], forKeys: ["key", "command"])
    let theImage = self.imageView1.image!
    let imageData = UIImageJPEGRepresentation(theImage, 0.6)
    let dictImage : NSMutableDictionary = NSMutableDictionary(object: imageData!, forKey: "productImage")
    I want to upload dictInfo and the dictImage.
  2. Dookieman macrumors 6502

    Oct 12, 2009
    Do you have an API that accepts an image? There are a variety of ways to upload an image and it depends on what the API expects.
  3. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    Actually the service i am working with that accepts image as form data and beside some more dictionary objects(e.g : name, surname, age etc). I can use that service with Objective C. Now i want to use it with Swift also. I tried something but couldnt create request body. I need help at that point.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
  5. Dookieman macrumors 6502

    Oct 12, 2009
    You can create a HTTPBody by using NSJSONSerialization and have it spit out NSData for you. But keep in mind that you need to structure your POST Request properly if it holds an image.

Share This Page