简体   繁体   English

iOS将数据发送到服务器

[英]IOS send data to server

I'm trying to send a image from mydevice to server use Socket. 我正在尝试使用Socket将图像从mydevice发送到服务器。

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. 看起来您正在将图像数据编码为String(base64?),然后将该NSDictionary编码为NSData并将其作为JSON对象发布到您的服务器。

Encoding images into strings will always produce a much larger content length that the data itself in image representation (see HERE ). 将图像编码为字符串将始终产生比图像表示中的数据本身更大的内容长度(请参见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. 没什么大不了的,只是您的服务器的API需要支持该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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