繁体   English   中英

通过iPhone中的套接字发送图像

[英]Send image over socket in iPhone

如何在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);

    }

}

问候,

该图像必须转换为base64字符串并通过socket传输。

这是Objective c中base-64转换的代码片段

- (NSString *)encodeToBase64String:(UIImage *)image {
 return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

迅速

let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

这是参考

暂无
暂无

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

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