【问题标题】:Reading large files from NSInputstream is not working in ios从 NSInputstream 读取大文件在 ios 中不起作用
【发布时间】:2015-09-21 05:54:31
【问题描述】:

我正在尝试从 NSInputStream 读取超过 300KB 的大型图像文件。但我只得到了300KB。其他数据丢失。如果您知道,请您帮帮我。我正在等待您的宝贵答复。 我在下面提到了我的代码: 从 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];

}

【问题讨论】:

  • “EAD_INPUT_BUFFER_SIZE”的大小是多少?
  • 1024,我也增加到了5120。但是没用

标签: ios iphone nsstream nsinputstream iphone-accessory


【解决方案1】:

当方法 hasBytesAvailable 返回 NO 时,并不总是意味着 NSInputStream 为空。 NSInputStream 仅在其状态为 NSStreamStatusAtEnd 时才结束。例如,从NSURL 构建的流是异步的,并且在打开后立即没有所有字节。如果这是您的情况,您应该订阅NSInputStream,将某个类声明为NSStreamDelegate

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多