Question

I'm using some code from Apple's "SquareCam" source code. It runs fine on iOS6, but on iOS5 I get a crash:

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];

This is the line it crashes on:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

This is the error message:

-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance

I don't really understand AVCapture that much in the first place. I'm just trying to take a picture. But why would this work fine on iOS6 but not iOS5?

Was it helpful?

Solution

The error message says:

-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance

So it's telling you that you can't say connection to an AVCaptureVideoPreviewLayer.

And indeed, the docs on AVCaptureVideoPreviewLayer say:

connection

Available in iOS 6.0 and later.

So there's the reason: in iOS 5 there's no connection property of an AVCaptureVideoPreviewLayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top