IOS如何在相机预览图层上合并一个图像?

时间:2017-06-06 09:22:37

标签: ios objective-c

我想在相机预览视图图层上添加一个png图像(蓝色矩形)。我从这个函数中获得了预览图像:

-(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    UIImage * image1 = [self ImageFromSampleBuffer:sampleBuffer];
    NSData *imageData1;
}

此功能是将预览图像设置为UIImageview。

-(void) SetPreview
{


    session =[[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device;
    AVCaptureDevice*device = nil;
    device = [self cameraWithPosition:AVCaptureDevicePositionFront];

    AVCaptureDeviceInput*input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if(!input)
    {
        NSLog(@"NO Input");
    }

    [session addInput:input];

    //Output

    output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings = @{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)};

    //Preview layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    cameraView = self.view;

    previewLayer.frame =CGRectMake(cameraView.bounds.origin.x+5, cameraView.bounds.origin.y+5, cameraView.bounds.size.width - 10, cameraView.bounds.size.height-10);

    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;


    [_vImage.layer addSublayer:previewLayer];
    timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(snapshot) userInfo:nil repeats:YES];

    [session startRunning];


}

如何实现此功能?

enter image description here

0 个答案:

没有答案