ios7 – 使用Multipeer Connectivity框架进行缓慢的文件传输

ios7 – 使用Multipeer Connectivity框架进行缓慢的文件传输,第1张

概述我正在使用iOS 7的Multiper Connectivity Framework在两台设备之间发送文件.我正在使用NSStreams来传输文件,就像我之前尝试使用MCSession的sendData:toPeers:withMode真的不可靠.不幸的是,我得到的传输速度真的很慢,大约100kb / s,这对于我正在开发的应用程序来说是不行的.这是我的输入和输出流委托方法,这是文件传输发生的地方 我正在使用iOS 7的Multiper Connectivity Framework在两台设备之间发送文件.我正在使用Nsstreams来传输文件,就像我之前尝试使用MCSession的sendData:topeers:withMode真的不可靠.不幸的是,我得到的传输速度真的很慢,大约100kb / s,这对于我正在开发的应用程序来说是不行的.这是我的输入和输出流委托方法,这是文件传输发生的地方.

输出流(在流的代表中)

...//prevIoUs code case NsstreamEventHasspaceAvailable: {            //we will only open the stream when we want to send the file.            NSLog(@"Stream has space available");            //[self updateStatus:@"Sending"];            // If we don't have any data buffered,go read the next chunk of data.            if (totalBytesWritten< outgoingDataBuffer.length) {                //more stuff to read                int towrite;                int diff = outgoingDataBuffer.length-packetSize;                if (diff <= totalBytesWritten)                {                    towrite = outgoingDataBuffer.length - totalBytesWritten;                } else                    towrite = packetSize;                NSRange byterange = {totalBytesWritten,towrite};                uint8_t buffer[towrite];                [outgoingDataBuffer getBytes:buffer range:byterange];                NSInteger bytesWritten = [outputStream write:buffer maxLength:towrite];                totalBytesWritten += bytesWritten;                NSLog(@"Written %d out of %d bytes",totalBytesWritten,outgoingDataBuffer.length);            } else {                //we've written all we can write about the topic?                NSLog(@"Written %d out of %d bytes",outgoingDataBuffer.length);                [self endStream];            }            // If we're not out of data completely,send the next chunk.        } break;

输入流

- (voID)stream:(Nsstream *)stream handleEvent:(NsstreamEvent)eventCode {    switch(eventCode) {        case NsstreamEventHasBytesAvailable:        {            NSLog(@"Bytes Available");            //Sent when the input stream has bytes to read,we need to read bytes or else this wont be called again            //when this happens... we want to read as many bytes as we can            uint8_t buffer[1024];            int bytesRead;            bytesRead = [inputStream read:buffer maxLength:sizeof(buffer)];            [incomingDataBuffer appendBytes:&buffer length:bytesRead];            totalBytesRead += bytesRead;            NSLog(@"Read %d bytes,total read bytes: %d",bytesRead,totalBytesRead);        }break;        case NsstreamEventEndEncountered:        {            UIImage *newImage = [[UIImage alloc]initWithData:incomingDataBuffer];            [[self.detailVIEwController imageVIEw] setimage:newImage];            NSLog(@"End Encountered");            [self closeStream];            //this should get called when there aren't any more bytes being sent down the stream        }    } }

有没有办法通过多线程或使用稍微修改的Nsstream子类来加速此文件传输,这些子类使用异步套接字?

解决方法 有几件事我注意到导致缓慢的文件传输:

>在会话中使用非空投兼容设备(http://en.wikipedia.org/wiki/AirDrop)
> WiFi问题(饱和网络,路由器问题,频道等)

iPhone上的iPhone 5S,iPad Air和iPhone模拟器之间的速度(以及体面的意思是500K /秒).

总结

以上是内存溢出为你收集整理的ios7 – 使用Multipeer Connectivity框架进行缓慢的文件传输全部内容,希望文章能够帮你解决ios7 – 使用Multipeer Connectivity框架进行缓慢的文件传输所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存