NSStream を介して毎秒 UIImages を送信しようとしています
しかし、デコード時にクラッシュします
NSData * data = [NSData dataWithBytes:buffer length:len];
*キャッチされない例外: *** -[NSKeyedUnarchiver initForReadingWithData:]: 理解できないアーカイブ (0x62、0x70、0x6c、0x69、0x73、0x74、0x30、0x30)*
取り扱いが悪いため、データが壊れている/不完全なようです
- 画像を送信するたびに、新しいストリームを作成する必要があるかどうかわかりません
- デコード、特にバッファ サイズ (およびサイズ変数を使用して動作させる方法) についてはわかりません
// UIImage を毎秒送信する
- (void)sendStreamImage:(UIImage *)image withOrientation:(UIInterfaceOrientation)orientation {
NSError * error;
NSOutputStream *outputStream = [self.session startStreamWithName:@"SnapClapStream" toPeer:[self.session.connectedPeers firstObject] error:&error];
// handle error if present
outputStream.delegate = self;
[outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
NSData * imageData = ...
NSDictionary * dictionary = @{@"imageData":imageData, @"orientation":@(orientation)};
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
[outputStream write:[data bytes] maxLength:[data length]];
}
ストリーム デリゲートで
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
if (eventCode == NSStreamEventHasBytesAvailable) {
if (![aStream isKindOfClass:[NSInputStream class]]) {
return;
}
NSInputStream * inputStream = (NSInputStream *)aStream;
NSInteger bufferSizeNumber = (25 * 1024);
NSMutableData *myBuffer = [NSMutableData dataWithLength:bufferSizeNumber];
uint8_t *buffer = [myBuffer mutableBytes];
NSInteger len = 0;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:bufferSizeNumber];
if (len > 0) {
NSData * data = [NSData dataWithBytes:buffer length:len];
NSDictionary * dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
dictionary = [self processReceivedDictionary:dictionary];
// thats it, dictionary should contain all the data
}
}
} else if (eventCode == NSStreamEventEndEncountered) {
...
}
}