【发布时间】:2015-07-18 00:39:52
【问题描述】:
我有一个应用程序,我在其中使用多点连接框架在多个设备(点)之间传输图像。我遇到的问题是正在传输的图像很慢。我会说一个 500kb 的图像大约需要 2 秒才能发送到对等点。
以下是我的应用架构
我有多个设备通过wifi相互连接(没有任何路由器。请注意,这些设备没有连接到任何外部网络。我只是打开wifi并没有连接到任何网络。)我不是确定为什么,但我必须在所有设备上启动蓝牙,以便它们被充当服务器并搜索其他对等方连接的设备发现。当我从服务器设备向对等方发送图像时,图像传输非常慢。我认为图像应该通过 wifi 通道传输,我想应该是 5 MB/秒的传输速度。我得到的是 2 秒内 500 KB。请检查我用于多点连接的代码。
发送图片代码
-(void)sendImage
{
UIGraphicsBeginImageContextWithOptions(_imgSize, NO, 0.0);
[fullImage drawInRect:CGRectMake(0, 0, _imgSize.width, _imgSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *sendData = UIImageJPEGRepresentation(fullImage, 0.2);
NSLog(@"uiimage IMAGE data:- %lu",(unsigned long)sendData.length);
NSArray *allPeers = appDelegate.mcManager.mySession.connectedPeers;
NSString *strDeviceInfo=appDelegate.mcManager.mySession.description;
NSLog(@"%@",strDeviceInfo);
NSError *error;
if(allPeers.count<=0)
{
[self.imgView setImage:fullImage];
}
else
{
[appDelegate.mcManager.mySession sendData:sendData toPeers:allPeers withMode:MCSessionSendDataReliable error:&error];
if (error)
{
NSLog(@"%@", [error localizedDescription]);
}
}
//imgView.layer.contentsRect=_myFrame;
imgView.image=fullImage;
imgView.backgroundColor=[UIColor darkGrayColor];
imgView.contentMode = UIViewContentModeScaleAspectFit;
}
接收图片代码
-(void)didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
NSDictionary *dict = @{@"data": data,@"peerID": peerID};
if([strPerform isEqualToString:@"orientation"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveContentSize" object:data userInfo:dict];
[clientViewObj receivedContentSize:data];
}
else if ([strPerform isEqualToString:@"image"])
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
UIImage *image = [UIImage imageWithData:data];
clientViewObj.imgView.image = image;
//[clientViewObj ReceivedData:data];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification" object:data userInfo:dict];
});
}
else if ([strPerform isEqualToString:@"pinch"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceivePinch" object:data userInfo:dict];
[clientViewObj pinched:data];
}
else if ([strPerform isEqualToString:@"change"])
{
NSDictionary *dict = @{@"data": data,@"peerID": peerID};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeProcess" object:data userInfo:dict];
}
else if([strPerform isEqualToString:@"client"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeServer" object:data userInfo:dict];
//[_orientationViewObj receiveFromClient];
}
else if([strPerform isEqualToString:@"up"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidRotateImageUp" object:data userInfo:dict];
}
else if([strPerform isEqualToString:@"down"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidRotateImageDown" object:data userInfo:dict];
}
}
我已经尝试了消息和资源类型来发送图像。资源比消息数据慢。
【问题讨论】:
-
对于图像,最好使用 'sendData' 或 'sendResourceAtURL'?每篇nshipster.com/multipeer-connectivity 的文章我一直在使用 sendResourceAtURL。我也遇到了图像发送和接收速度非常慢的问题。
标签: ios ios7 data-transfer multipeer-connectivity