Sending UIImage to MySQL

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

  1. man2manno macrumors member

    Mar 21, 2009
    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

    Feb 5, 2008
    Battle Ground
    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


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


    Jun 15, 2000
    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

    Mar 21, 2009
    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.
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
    		[req setHTTPMethod:@"POST"];
    		[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [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


    Staff Member

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

Share This Page