开发者

Send image over socket in iPhone

How can i send image over socket in iPhone. I tried with string the code i used is as follows, now i need to send image over socket.

CFWriteStreamRef writeStream = NULL;

CFStringRef host = CFSTR("192.168.1.211");
UInt32开发者_JAVA技巧 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);

    }

}

Regards,


The image has to be converted to base64 string and transferred through socket.

Here is the code snippet for base-64 conversion in Objective c

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

Swift

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

Here is the reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜