简体   繁体   English

将iphone相机集成到ViewController的子视图UIView中

[英]Integrate iphone camera into ViewController's subview UIView

I am using following code to embed Camera into my application view. 我正在使用以下代码将Camera嵌入到我的应用程序视图中。

Here is my code 这是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.cameraView.bounds;
    [self.cameraView.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input)
    {
        [Utilities alertDisplay:@"Error" message:@"Camera not found. Please use Photo Gallery instead."];
    }

    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];
}

-(AVCaptureDevice *)backFacingCameraIfAvailable{

    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;

    for (AVCaptureDevice *device in videoDevices){

        if (device.position == AVCaptureDevicePositionBack){

            captureDevice = device;
            break;
        }
    }

    //  couldn't find one on the front, so just get the default video device.
    if (!captureDevice){

        captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }

    return captureDevice;
}


- (IBAction)scanButtonPressed:(id)sender
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             NSLog(@"attachements: %@", exifAttachments);
         }
         else
             NSLog(@"no attachments");

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         //self.vImage.image = image;
     }];
}

The problem i am facing is, I don't get any camera opened in my cameraView and also on scanBtnPressed i get 我面临的问题是,我没有在我的cameraView中打开任何相机,也在scanBtnPressed上我得到了

stillImageOutput.connections = 0 objects.

What is wrong? 怎么了?

Well, I just copy pasted your code into a blank project and it worked fine when changing self.cameraView.layer to self.view.layer . 好吧,我只是复制粘贴代码到一个空白的项目,它改变时,工作得很好self.cameraView.layerself.view.layer However, I did try creating self.cameraView and never initializing it and it had similar consequences to those you described. 但是,我确实尝试创建self.cameraView并且从不初始化它,它与你描述的那些有类似的结果。

Overall, I would check to make sure that self.cameraView isn't nil. 总的来说,我会检查以确保self.cameraView不是零。 If it's done programmatically, make sure you're calling alloc/init and setting a frame, and if it's an IBOutlet make sure it's properly linked. 如果以编程方式完成,请确保调用alloc / init并设置框架,如果是IBOutlet,请确保它已正确链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM