Iβve been knocking on this head for several days now.
I want to draw a rectangle on top of CALayer (AVCaptureVideoPreviewLayer), which is just the video stream from the camera on iPhone4.
here is part of my setup;
//(in function for initialization) -(void)initDevices { AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; captureOutput.alwaysDiscardsLateVideoFrames = YES; captureOutput.minFrameDuration = CMTimeMake(1, 30); dispatch_queue_t queue; queue = dispatch_queue_create("cameraQueue", NULL); [captureOutput setSampleBufferDelegate:self queue:queue]; dispatch_release(queue); NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; [captureOutput setVideoSettings:videoSettings]; self.captureSession = [[AVCaptureSession alloc] init]; [self.captureSession addInput:captureInput]; [self.captureSession addOutput:captureOutput]; [self.captureSession setSessionPreset:AVCaptureSessionPresetHigh]; self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; self.prevLayer.frame = CGRectMake(0, 0, 400, 400); self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; self.prevLayer.delegate = self; [self.view.layer addSublayer: self.prevLayer]; } - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { [self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES]; } - (void)drawme { [self.prevLayer setNeedsDisplay]; } //delegate function that draws to a CALayer - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { NSLog(@"hello layer!"); CGContextSetRGBFillColor (ctx, 1, 0, 0, 1); CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100 )); }
Is it possible? From my current code I get a "hello layer" print, but there is no filled rectangle in the camera feed.
Any help would be awesome. :)
ios camera drawing iphone-4
Slade villena
source share