AVCaptureVideoPreviewLayer连接仅在iOS5上崩溃

时间:2013-05-17 23:48:46

标签: ios ios5 avcapturesession unrecognized-selector avcapture

我正在使用Apple的“SquareCam”源代码中的一些代码。它在iOS6上运行良好,但在iOS5上我遇到了崩溃:

AVCaptureSession *session = [AVCaptureSession new];
[session setSessionPreset:AVCaptureSessionPresetPhoto];


// Select a video device, make an input
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

require( error == nil, bail );
{

isUsingFrontFacingCamera = NO;
if ( [session canAddInput:deviceInput] )
    [session addInput:deviceInput];

// Make a still image output
self.stillImageOutput = [AVCaptureStillImageOutput new];
[self.stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:(__bridge void *)(AVCaptureStillImageIsCapturingStillImageContext)];
if ( [session canAddOutput:self.stillImageOutput] )
    [session addOutput:self.stillImageOutput];

// Make a video data output
self.videoDataOutput = [AVCaptureVideoDataOutput new];

// we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA'
NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
                                   [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[self.videoDataOutput setVideoSettings:rgbOutputSettings];
[self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image)


// create a serial dispatch queue used for the sample buffer delegate as well as when a still image is captured
// a serial dispatch queue must be used to guarantee that video frames will be delivered in order
// see the header doc for setSampleBufferDelegate:queue: for more information
videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
[self.videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];

if ( [session canAddOutput:self.videoDataOutput] )
    [session addOutput:self.videoDataOutput];
[[self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:NO];


effectiveScale = 1.0;
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
//[self.previewLayer setBackgroundColor:[[UIColor redColor] CGColor]];
//self.previewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];


//Get Preview Layer connection - this is for orientation purposes
AVCaptureConnection *previewLayerConnection=self.previewLayer.connection; //THIS CRASHES ON IOS5


if ([previewLayerConnection isVideoOrientationSupported])
    [previewLayerConnection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

CALayer *rootLayer = [self.previewView layer];
[rootLayer setMasksToBounds:YES];
[self.previewLayer setFrame:self.view.bounds];

[rootLayer addSublayer:self.previewLayer];

[session startRunning];

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

这是它崩溃的路线:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

这是错误消息:

-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance

我一开始并不太了解AVCapture。我只想拍张照片。但是为什么这在iOS6上工作得很好而不是iOS5?

1 个答案:

答案 0 :(得分:2)

错误消息显示:

  

- [AVCaptureVideoPreviewLayer连接]:无法识别的选择器发送到实例

所以它告诉你,你不能对AVCaptureVideoPreviewLayer说connection

事实上,AVCaptureVideoPreviewLayer上的文档说:

  

<强>连接

     

适用于iOS 6.0及更高版本。

所以原因是:在iOS 5中,AVCaptureVideoPreviewLayer没有connection属性。