我成功发送了NSData流。下面的委托(delegate)方法是获取该流并将其附加到NSMutableData self.data。如何获取这些数据并将其放入UIView/AVCaptureVideoPreviewLayer(应显示视频)中?我觉得我想念另一种转换,AVCaptureSession> NSStream> MCSession> NSStream>吗?
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if(!self.data) {
self.data = [NSMutableData data];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
[self.data appendBytes:(const void *)buf length:len];
} else {
NSLog(@"no buffer!");
}
// Code here to take self.data and convert the NSData to UIView/Video
}
我发送与此流:
-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
void *src_buff = CVPixelBufferGetBaseAddress(imageBuffer);
NSData *data = [NSData dataWithBytes:src_buff length:bytesPerRow * height];
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
NSError *error;
self.oStream = [self.mySession startStreamWithName:@"videoOut" toPeer:[[self.mySession connectedPeers]objectAtIndex:0] error:&error];
self.oStream.delegate = self;
[self.oStream scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[self.oStream open];
[self.oStream write:[data bytes] maxLength:[data length]];
// CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );
CGSize imageSize = CVImageBufferGetEncodedSize( imageBuffer );
// also in the 'mediaSpecific' dict of the sampleBuffer
NSLog( @"frame captured at %.fx%.f", imageSize.width, imageSize.height );
}
最佳答案
我认为您需要AVCaptureManager
,看看下面的代码是否适合您。
AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
[self setCaptureManager:manager];
[[self captureManager] setDelegate:self];
if ([[self captureManager] setupSession]) {
// Create video preview layer and add it to the UI
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
UIView *view = self.videoPreviewView;//Add a view in XIB where you want to show video
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[newCaptureVideoPreviewLayer setFrame:bounds];
[newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
[self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[[self captureManager] session] startRunning];
});
}
管理代表
- (void)captureManager:(AVCamCaptureManager *)captureManager didFailWithError:(NSError *)error
{
}
- (void)captureManagerRecordingBegan:(AVCamCaptureManager *)captureManager
{
}
- (void)captureManagerRecordingFinished:(AVCamCaptureManager *)captureManager outputURL:(NSURL *)url
{
}
- (void)captureManagerStillImageCaptured:(AVCamCaptureManager *)captureManager
{
}
- (void)captureManagerDeviceConfigurationChanged:(AVCamCaptureManager *)captureManager
{
}
希望对您有所帮助。
关于ios - 将传入的NSStream转换为View,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22390679/