简体   繁体   中英

IOS send data to server

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM