我正在尝试使用 NSOuptutStream 的写入方法将大型图像从服务器发送到客户端。但是我发现在写入 684032 字节后,流不再写入字节。我觉得这特别奇怪。我能够发送尺寸 < 680432 的图像并在客户端正确接收它们。下面附上代码片段。如果有人能指出我到底哪里出错了,那就太好了。
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] 服务器:写入字节数 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] 服务器:写入的字节数 1024
2012-07-04 13: 53:20.906 BasicClientServer[1707:f803] self.bufferoffset 684032 self.bufferlimit 1334301
并且不会超出这一点