Pregunta

I am using Apple MultipeerConnectivity framework and I am able to send the message to all the peers connected to it.

I am using:

- (BOOL)sendData:(NSData *)data toPeers:(NSArray *)peerIDs withMode:(MCSessionSendDataMode)mode error:(NSError **)error;

I am even able to send the image by converting it in NSData.

Problem is I am trying to send image and text together to other peers. I know there are three ways to send information to other peers:

1. NSData

2. Resource

3. Stream

So for sending an image and text together which method we should use and how?

Thanks

¿Fue útil?

Solución

You could use NSData for your task. Here is example:
Serialization to NSData:

NSString *string = @"Any String";
UIImage *image = [UIImage imageNamed:@"Any Image"];
NSData *imageData = UIImagePNGRepresentation(image);
NSDictionary *dictionary = @{@"string":string, @"imageData":imageData};
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
// Send |data| by MultipeerConnectivity

Deserialization to NSString and UIImage:

// Receive |data| by MultipeerConnectivity
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSString *string = dictionary[@"string"];
NSData *imageData = dictionary[@"imageData"];
UIImage *image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top