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?