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

    Joined:
    Jul 20, 2011
    #1
    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
    Code:
    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

    Joined:
    Oct 12, 2009
    #2
    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

    Joined:
    Jul 20, 2011
    #3
    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 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  5. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #5
    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