0

I'm trying to send a image from mydevice to server use Socket.

I used this code to convert from image to bytes. After that, I sent bytes to server:

- (IBAction)btnSend:(id)sender {

    UIImage *image = [UIImage imageNamed:@"button.png"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
//    NSLog(@"data: %@", imageData);
    NSString *string = [NSString stringWithFormat:@"%@", imageData];
    NSDictionary *deviceDic = @{@"RequestType": @"5",
                                @"StringData":string};
    NSData* bodyData = [NSJSONSerialization dataWithJSONObject:deviceDic
                                                       options:kNilOptions error:nil];
    [socket writeData:bodyData withTimeout:-1 tag:0];
}

But, my bytes array is very big so, I want to send ever bytes to server.

rtruszk
  • 3,902
  • 13
  • 36
  • 53
user5068763
  • 355
  • 1
  • 3
  • 10
  • 5
    Why read the `.png` into a `UIImage` object only to generate a PNG representation straight away? What's wrong with reading the file directly into an `NSData` object? You are also certainly not formatting the data object correctly in the socket request. What protocol are you using? – Droppy Jul 01 '15 at 08:55
  • He probably means a partial content technique. what protocol do you use when communicating via socket? is it http? – heximal Jul 01 '15 at 09:28
  • What is `socket`? What doesn't work? What error do you get? – Christian Schnorr Jul 01 '15 at 12:49

1 Answers1

0

It looks like you are encoding your image data into a String (base64?), and then encoding that NSDictionary into NSData and posting that to your server as a JSON object.

Encoding images into strings will always produce a much larger content length that the data itself in image representation (see HERE). So if you want to send less data to your server, you will need to find a way to upload your image straight in binary format. Not a big deal, just that your server's API needs to support that functionality.

Community
  • 1
  • 1
Luis Delgado
  • 3,644
  • 4
  • 34
  • 54