如何在iPhone上通过套接字发送图像。我尝试使用字符串我使用的代码如下,现在我需要通过套接字发送图像。
CFWriteStreamRef writeStream = NULL;
CFStringRef host = CFSTR("192.168.1.211");
UInt32 port = 8001;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, NULL, &writeStream);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
if(!CFWriteStreamOpen(writeStream)) {
NSLog(@"Error Opening Socket");
}
else{
UInt8 buf[] = "Abc";
int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
NSLog(@"Written: %d", bytesWritten);
if (bytesWritten < 0) {
CFStreamError error = CFWriteStreamGetError(writeStream);
}
}
此致
答案 0 :(得分:1)
图片必须转换为 base64 字符串并通过 socket 传输。
以下是Objective c
中base-64转换的代码段- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
夫特
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
以下是reference