두 개의 NSInputStream
과 NSOutputStream
이 네트워크를 통해 서로 연결되어 있습니다. 하나의 장치에서 다른 장치로 핵심 데이터 개체 및 관련 이미지를 전송하려고합니다. 핵심 데이터 개체를 JSON
으로 변환하고 스트림의 다른 쪽으로 전송 한 다음 JSON
의 핵심 데이터를 채 웁니다. 이제 각 이미지와 관련된 이미지가 있습니다. 이미지는 디스크에 있으며 핵심 데이터 개체에 저장된 경로 만 있습니다. 이제 output stream
에 글을 쓰고있을 때 완벽한 데이터를 가져야합니다. 나는 XML
(즉, JSON
포함)을 가지고 있습니다.
1. 그러나 XML
(NSData *
)과 함께 이미지 (NSData *
)를 어떻게 전송합니까? XML과 이미지 사이의 읽기 끝 (NSInputStream
)에서 어떻게 구별 할 수 있습니까?
2. 또한 여러 이미지를 전송해야합니다. NSInputStream
끝에서 이미지의 바이트가 끝났으며 다음 이미지의 바이트가 시작되었음을 어떻게 알 수 있습니까?
3. 어떤 이미지 (이름)가 이전되었는지 어떻게 알 수 있습니까? NSString
표현
감사 (각 UIImage
의)NSInputStream/NSOutputStream을 통해 여러 이미지 전송
1
A
답변
0
나는 다음 단계를 사용하여 그것을 해결 : 변환 NSArray
3. 모든 모든 사전을 넣어 NSDictionary
2로
1. 변환 각 관리 객체를 NSArray
NSKeyedArchiver
를 사용 NSData
에 4. 스트림을 통해 NSData
을 전송하십시오.
수신자 측에서는 위의 teps.
감사합니다 Marius Kurgonas
0
변환 NSData
후 NSDictionary
에 모든 NSString
물건을 올려 그 사전을 직렬화. 그렇게하면 데이터를 전송할 때 어떤 이미지에 어떤 키 포인트가 있는지 알면서 이미지를 추출하는 프로세스를 되돌릴 수 있습니다. 그렇게하면 여러 이미지를 전송할 수 있습니다.
희망이 도움이됩니다.
건배
0
우스운 소리, 모든 대답. 이런 식으로 뭔가를 시도 :
case NSStreamEventHasBytesAvailable: {
NSLog(@"NSStreamEventHasBytesAvailable");
uint8_t * mbuf[DATA_LENGTH];
mlen = [(NSInputStream *)stream read:(uint8_t *)mbuf maxLength:DATA_LENGTH];
NSLog(@"mlen == %lu", mlen);
[mdata appendBytes:(const void *)mbuf length:mlen];
NSLog(@"mdata length == %lu", mdata.length);
if (mlen < DATA_LENGTH) {
NSLog(@"displayImage");
UIImage *image = [UIImage imageWithData:mdata];
[self.peerConnectionViewController.view.subviews[0].layer setContents:(__bridge id)image.CGImage];
mdata = nil;
mlen = DATA_LENGTH;
mdata = [[NSMutableData alloc] init];
}
} break;
...
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image = [[UIImage alloc] initWithCGImage:newImage scale:1 orientation:UIImageOrientationUp];
CGImageRelease(newImage);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.25)];
__block BOOL baseCaseCondition = NO; // obviously this should be data driven, not hardcoded
__block NSInteger _len = DATA_LENGTH;
__block NSInteger _byteIndex = 0;
typedef void (^RecursiveBlock)(void (^)());
RecursiveBlock aRecursiveBlock;
aRecursiveBlock = ^(RecursiveBlock block) {
NSLog(@"Block called...");
baseCaseCondition = (data.length > 0 && _byteIndex < data.length) ? TRUE : FALSE;
if ((baseCaseCondition) && block)
{
_len = (data.length - _byteIndex) == 0 ? 1 : (data.length - _byteIndex) < DATA_LENGTH ? (data.length - _byteIndex) : DATA_LENGTH;
//
NSLog(@"START | byteIndex: %lu/%lu writing len: %lu", _byteIndex, data.length, _len);
//
uint8_t * bytes[_len];
[data getBytes:&bytes range:NSMakeRange(_byteIndex, _len)];
_byteIndex += [self.outputStream write:(const uint8_t *)bytes maxLength:_len];
//
NSLog(@"END | byteIndex: %lu/%lu wrote len: %lu", _byteIndex, data.length, _len);
//
dispatch_barrier_async(dispatch_get_main_queue(), ^{
block(block);
});
}
};
if (self.outputStream.hasSpaceAvailable)
aRecursiveBlock(aRecursiveBlock);
}
감사합니다 사랑 . 당신의 대답은 저를 해결책으로 이끌었습니다. –