Sending UIImage to MySQL

Discussion in 'iOS Programming' started by man2manno, Dec 15, 2010.

  1. man2manno macrumors member

    Joined:
    Mar 21, 2009
    #1
    Hey guys, does anybody know how to send an image (from a UIImageView) to a mysql database and then pull it back down? I know to do with text but when it comes to the image I get stuck. Any ideas? Thank you.
     
  2. zachsilvey macrumors 6502

    Joined:
    Feb 5, 2008
    Location:
    Battle Ground
    #2
    I'm not an expert with database stuff but wouldn't you be better off storing the image on the file system and only storing the path to that file in the database?
     
  3. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    Store the pixel data as a BLOB object and then reload the pixel data. What part are you stuck on ?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    You probably are better off storing it in the file system and keeping a reference to it in the database, but if you still want to store it in the database, easiest would be to convert to jpeg or png via UIImageJPEGRepresentation()/UIImagePNGRepresentation() and +[UIImage imageWithData:]
     
  5. man2manno thread starter macrumors member

    Joined:
    Mar 21, 2009
    #5
    Ya I can convert it to the BLOB type however, I am sending it image to the server along with some text and when I go to send to them together it sends two different sets of data when I only want it to send one set of data.
    Code:
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
    		[req setHTTPMethod:@"POST"];
    		[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    Code:
    [req setValue:length forHTTPHeaderField:@"Content-Length"];
    		[req setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
    The above code is how I am sending my text data, somehow I want to incorporate the image into my postData (which is an NSString)
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Well, postData may be an NSString but calling that instance method (dataUsingEncoding:) returns an NSData.
     

Share This Page