문제

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

도움이 되었습니까?

해결책

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top