无法将数据写入NSOutputStream

时间:2012-07-04 18:05:22

标签: iphone objective-c ios cocoa-touch nsoutputstream

我正在尝试使用NSOuptutStream的write方法从服务器向客户端发送大尺寸图像。但是我发现在写入684032字节之后,流不再写入更多字节。我发现这特别奇怪。我能够发送大小

UIImage *img;
img = [UIImage imageWithContentsOfFile:path];
NSMutableData *imgData = [UIImageJPEGRepresentation(img, 1.0) mutableCopy];

const void *bytes = [imgData mutableBytes];
NSInteger length = [imgData length];
self.bufferLimit = length;
NSLog(@"self.bufferoffset %d self.bufferlimit %d", self.bufferOffset, self.bufferLimit);

uint8_t *crypto_data = (uint8_t *)bytes;                   
NSInteger   bytesWritten;

if([self.outputStream hasSpaceAvailable]) {
    NSLog(@"Space Available");
} else NSLog(@"Space Not Available");

while (self.bufferOffset < self.bufferLimit) {

    bytesWritten = [self.outputStream write:(uint8_t *)crypto_data maxLength:1024];
    NSLog(@"server: bytes weritten %d",bytesWritten);
    assert(bytesWritten != 0);
    if (bytesWritten == -1) {
        [self stopSendWithStatus:@"output buffer write error"];
    } else {
        self.bufferOffset += bytesWritten;
    }
    NSLog(@"self.bufferoffset %d self.bufferlimit %d", self.bufferOffset, self.bufferLimit);
} 
NSLog(@"Done sending");
[self.outputStream close]; 

当我的代码挂起时,这是我留下的输出

...
2012-07-04 13:53:20.903 BasicClientServer [1707:f803] self.bufferoffset 681984 self.bufferlimit 1334301
2012-07-04 13:53:20.903 BasicClientServer [1707:f803]服务器:bytes weritten 1024
2012-07-04 13:53:20.904 BasicClientServer [1707:f803] self.bufferoffset 683008 self.bufferlimit 1334301
2012-07-04 13:53:20.905 BasicClientServer [1707:f803]服务器:bytes weritten 1024
2012-07-04 13:53:20.906 BasicClientServer [1707:f803] self.bufferoffset 684032 self.bufferlimit 1334301

并没有超越这一点

0 个答案:

没有答案