简体   繁体   English

在iOS中无法从NSInputstream读取大文件

[英]Reading large files from NSInputstream is not working in ios

I am trying to read large Image file more than 300KB from NSInputStream. 我正在尝试从NSInputStream读取大于300KB的大型图像文件。 But i'm getting only upto 300KB. 但是我最多只能得到300KB。 Other data are missing. 其他数据丢失。 Could you please help me if you know. 如果您知道的话,请您能帮助我。 i'm waiting for your valuable answer. 我正在等待您的宝贵答复。 I mentioned my code below : Call this readAllData method from NSStreamEventsHasBytesAvailable: 我在下面提到了我的代码:从NSStreamEventsHasBytesAvailable调用此readAllData方法:

    - (void)readAllData {

    if (_readData == nil) {
    _readData = [[NSMutableData data] retain];
    }

    while ([[_session inputStream] hasBytesAvailable])
    {
    unsigned   int bytesRead = 0;
    bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];

    if (bytesRead) {

        NSMutableString *_string = [NSMutableString stringWithString:@""];
        for (int i = 0; i < _readData.length; i++) {
            unsigned char _byte;
            [_readData getBytes:&_byte range:NSMakeRange(i, 1)];
            if (_byte >= 32 && _byte < 127) {
                [_string appendFormat:@"%c", _byte];
            } else {
                [_string appendFormat:@"[%d]", _byte];
            }
        }

        [_readData appendBytes:(const void *)buf length:bytesRead];

    }

}

NSString *string=[NSString stringWithFormat:@"%d",_readData.length];
UIAlertView *sesView = [[UIAlertView alloc] initWithTitle:@"readDatalength"
                                                  message:string
                                                 delegate:self
                                        cancelButtonTitle:nil otherButtonTitles:@"OK"  , nil];
[sesView show];

} }

When method hasBytesAvailable returns NO it doesn't always mean, that NSInputStream is empty. 当方法hasBytesAvailable返回NO时,它并不总是表示NSInputStream为空。 NSInputStream is at end only when its status is NSStreamStatusAtEnd . 仅当NSInputStream的状态为NSStreamStatusAtEnd时,它才处于结尾。 For example a stream which was been built from NSURL is asynchronous and it has no all bytes immediately after opening. 例如,从NSURL构建的流是异步的,并且在打开后没有立即所有字节。 If that is your case you should subscribe to NSInputStream declaring some class as NSStreamDelegate . 如果是这种情况,您应该订阅NSInputStream ,将某些类声明为NSStreamDelegate

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

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