iphone - 将iphone摄像头集成到ViewController的 subview UIView中

标签 iphone objective-c cocoa-touch

我正在使用以下代码将相机嵌入到我的应用程序 View 中。

这是我的代码

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

我面临的问题是,我没有在我的 cameraView 和 scanBtnPressed 上打开任何相机

stillImageOutput.connections = 0 objects.

怎么了?

最佳答案

好吧,我只是将您的代码复制粘贴到一个空白项目中,将 self.cameraView.layer 更改为 self.view.layer 时效果很好。但是,我确实尝试创建 self.cameraView 但从未对其进行初始化,结果与您描述的结果类似。

总的来说,我会检查以确保 self.cameraView 不为零。如果它是以编程方式完成的,请确保您正在调用 alloc/init 并设置一个框架,如果它是一个 IBOutlet,请确保它已正确链接。

关于iphone - 将iphone摄像头集成到ViewController的 subview UIView中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230490/

相关文章:

ios - 共享后执行 Segue - Objective-C (Xcode)

iphone - UITableViewCell 内 UIButton 的 IBAction

iphone - 获得在 iPhone 中播放的音乐的力量

ios - 如何在 prepareForSegue 方法中访问对象数组中选定对象的不同属性或特性

ios - 在 UIImageView 的右上角添加 UIButton

objective-c - delaysContentTouches 仅在特定元素上而不进行子类化?

ios - 从 Project 导入时,Xcode Image Assets Catalog 无法看到所有图像?

iphone - 制作视频缩略图时CMTimeMake崩溃

iphone - 尝试定义 rightBarButtonItems 时应用程序崩溃

iphone - UITableView scrollIndicator 位置不正确