我正在iPhone上編程客戶端。我想發送一些字符串,並從服務器接收圖像。在iPhone上接收圖像(TCP客戶端)
我發現本教程(http://www.devx.com/wireless/Article/43551),它一直非常有用。它可以工作,如果我想接收字符串,但現在我想收到一個圖像,我不能讓它工作。
這是我的代碼,當iPhone收到數據。這是本教程和JeremyP(http://stackoverflow.com/questions/4613218)這個回答讓混合:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasBytesAvailable: {
if (data == nil) {
data = [[NSMutableData alloc] init];
}
uint8_t buf[102400];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:102400];
if(len) {
[data appendBytes:(const void *)buf length:len];
// Recibir img
uint8_t size; // Let's make sure size is an explicit width.
NSInteger totalBytesRead = 0;
NSInteger bytesRead = [iStream read: &size maxLength: sizeof size];
while (bytesRead > 0 && totalBytesRead + bytesRead < sizeof size) {
totalBytesRead+= bytesRead;
bytesRead = [iStream read: &size + totalBytesRead maxLength: (sizeof size) - totalBytesRead];
}
if (bytesRead >= 0) {
totalBytesRead += bytesRead;
}
else {
// read failure, report error and bail
}
if (totalBytesRead < sizeof size) {
// connection closed before we got the whole size, report and bail
}
size = ntohl(size); // assume wire protocol uses network byte ordering
NSMutableData* buffer = [[NSMutableData alloc] initWithLength: size];
totalBytesRead = 0;
bytesRead = [iStream read: [buffer mutableBytes] maxLength: size];
while (bytesRead > 0 && totalBytesRead + bytesRead < size) {
totalBytesRead+= bytesRead;
bytesRead = [iStream read: [buffer mutableBytes] + totalBytesRead maxLength: size - totalBytesRead];
}
if (bytesRead >= 0) {
totalBytesRead += bytesRead;
}
else {
// read failure, report error and bail (not forgetting to release buffer)
}
if (totalBytesRead < size) {
// connection closed before we got the whole image, report and bail (not forgetting to release buffer)
}
else {
[buffer setLength: size];
}
imgResultado.image = [UIImage imageWithData: buffer];
[buffer release];
[data release];
data = nil;
}
else {
NSLog(@"No data.");
}
} break;
}}
它不工作...你能幫助我嗎?
這意味着您的服務器不像您之前的代碼所期望的那樣在流的開始處傳輸大小整數。這就是爲什麼你以前的代碼不起作用,因爲如果圖像數據有效地丟棄了前4個字節。 – kamprath 2012-02-27 02:19:47
因此,使用此代碼,您可以接收高達1000000字節的圖像。這差不多是1MB? (您的原始代碼只有大約100k?)只要嘗試確認... – user523234 2012-02-27 04:05:27
user523234是的,客戶端可以接收圖像高達1000000字節。 Claireware,這可能是一個很好的解釋,我認爲你是對的。這種方式比我的答案更安全,但如果我想使用它,我需要修改服務器。 – Xithias 2012-02-27 08:40:32