简体   繁体   English

ios Multipeer Connectivity通过wifi传输图像缓慢

[英]ios Multipeer Connectivity slow image transfer through wifi

I have an app where I am using multipeer connectivity framework to transfer image between multiple devices (peers). 我有一个应用程序,使用多对等连接框架在多个设备(对等)之间传输图像。 The issue that I have is that the image being transfer is slow. 我的问题是正在传输的图像很慢。 I would say a 500kb image takes about 2 secs to be sent over to the peers. 我想说一个500kb的图像大约需要2秒钟才能发送到同级。

Following is my app architecture 以下是我的应用架构

I have multiple devices connecting to each other through wifi (without any router. Please note that the devices are not connected to any external network. I just turn on the wifi and does not connect it to any network.) I am not sure why but I have to start bluetooth as well on all the devices for them to be discovered by the device which is acting as a server and searching for other peers to connect. 我有多个设备通过wifi相互连接(没有任何路由器。请注意,这些设备未连接到任何外部网络。我只是打开了wifi并没有将其连接到任何网络。)我不确定为什么,但是我还必须在所有设备上启动蓝牙,以使它们被充当服务器并搜索其他对等设备的设备发现它们。 When I send an image from the server device to the peers, the image transfer is really slow. 当我从服务器设备向对等方发送图像时,图像传输确实很慢。 I thought the image should be transferring through the wifi channel which I suppose should be somewhere 5 MB/sec transfer speed. 我以为图像应该通过wifi通道传输,我认为应该是5 MB /秒的传输速度。 What I get is 500 KB in 2 seconds. 我得到的是2秒内500 KB。 Please check my code that I am using for multipeer connection. 请检查我用于多对等连接的代码。

Send Image code 发送图片代码

  -(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;
    }

Receive Image code 接收图片代码

-(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];
    }
}

I have tried the message and resource types to send the image. 我尝试了消息和资源类型来发送图像。 The resource is slower then message data. 资源比消息数据慢。

In Multipeer Connectivity, transfers happen between both Bluetooth and WiFi. 在Multipeer Connectivity中,蓝牙和WiFi之间都会进行传输。 You can't guarantee any of the methods of data transfer, so if one device is on a WiFi network, the method of transfer will default to Bluetooth (slow transfers). 您无法保证任何一种数据传输方法,因此,如果一个设备在WiFi网络上,则传输方法将默认为蓝牙(慢速传输)。

Wifi on iOS 8 appears to be much slower. iOS 8上的Wifi似乎慢得多。

Today I implemented two apps that communicate together. 今天,我实现了两个可相互通信的应用程序。 One sends Core Motion data to the other via a Multipeer Connectivity session stream (NSOutputStream/NSInputStream). 一个通过Multipeer Connectivity会话流(NSOutputStream / NSInputStream)将Core Motion数据发送给另一个。

With Wifi disabled on either device it flies on Bluetooth. 在任一设备上均禁用Wifi时,它会通过蓝牙运行。 Frequency was 50 gyroo motion objects a second from the Core Motion hardware and the connected devices was receiving 50 values a second. 每秒从Core Motion硬件发出50个gyroo运动对象的频率,并且连接的设备每秒接收50个值。

On Wifi it's very slow, maybe 4–5x? 在Wifi上速度非常慢,可能是4-5倍? Same code. 相同的代码。 No idea why. 不知道为什么。 It defaults to Wifi, so if both devices have it enabled it seems to try that first. 它默认为Wifi,因此如果两个设备都启用了Wifi,则似乎首先尝试该操作。

I'm going to install iOS 9 beta tomorrow to see if they address this issue. 我明天将安装iOS 9 beta,以查看它们是否解决了此问题。

This is an old question, but since I just dealt with the same problem, I thought my experience might help someone else. 这是一个古老的问题,但是由于我只是处理相同的问题,所以我认为我的经验可能会对其他人有所帮助。

In my case, sending the image using a stream rather than the sendData method works about twice as fast. 就我而言,使用流而不是sendData方法发送图像的速度大约是以前的两倍。 (Still note quite as fast as I would prefer, but this is a sizable gain). (仍然请注意我想要的速度,但这是一笔可观的收益)。

Send function 发送功能

func sendImage(imageURL: URL) {
    DispatchQueue.main.async {
        do {
            try self.mcSession.startStream(withName: "Image", toPeer: self.mcSession.connectedPeers[0])
            self.mcSession.sendResource(at: imageURL, withName: fileName, toPeer: self.mcSession.connectedPeers[0], withCompletionHandler: nil)
        }
        catch let error {
            print("error: \(error)")
        }
    }
}

Receive function 接收功能

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
    do {
        let localData = try Data(contentsOf: localURL!)
        let image = UIImage(data: localData)
        DispatchQueue.main.async {
            // DispatchQueue.main.async necessary because modifying UI
            self.imageView.image = image
        }
    }
    catch let error {
        print("error \(error)")
    }
}

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

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