How to convert image into binary format in iOS? -


i working on project need upload image server. want store image's binary data blob data type field in database. therefore need convert image binary format. can saved on servers database.

so how convert image binary format? please advise.

you can use coregraphics' method uiimagepngrepresentation(uiimage *image), returns nsdata , save it. , if want convert again uiimage create using [uiimage imagewithdata:(nsdata *data)] method.

demo send uiimage server

- (void)sendimagetoserver {        uiimage *yourimage= [uiimage imagenamed:@"image.png"];        nsdata *imagedata = uiimagepngrepresentation(yourimage);        nsstring *postlength = [nsstring stringwithformat:@"%d", [imagedata length]];         // init urlrequest        nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init];        [request sethttpmethod:@"post"];        [request seturl:[nsurl urlwithstring:[nsstring stringwithstring:@"http://yoururl.domain"]]];        [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"];        [request setvalue:postlength forhttpheaderfield:@"content-length"];        [request sethttpbody:imagedata];         nsurlconnection *connection = [[nsurlconnection alloc] initwithrequest:request delegate:self];        if (connection) {           // response data of request        }        [request release];  } 

Comments