【问题标题】:Custom Camera View Not Working on iOS 8/Xcode 6自定义相机视图在 iOS 8/Xcode 6 上不起作用
【发布时间】:2014-11-17 03:47:48
【问题描述】:

因此,当我在手机上安装 iOS 7 和 Xcode 5.1(现在在 iOS 8 和 Xcode 6 上)时,我在我的另一个应用程序上使用此代码在自定义相机视图上拍照,相机可以工作,但我不能在我的 leftVertical UIView 中查看相机的实时视图。这是我的代码,不胜感激谢谢!

#import <AVFoundation/AVFoundation.h>

session = [[AVCaptureSession alloc] init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    [session setSessionPreset:AVCaptureSessionPreset352x288];
else
    [session setSessionPreset:AVCaptureSessionPreset352x288];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
    [session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.leftVertical.frame;

[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];

//////////////////////////

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

[session addOutput:stillImageOutput];

[session startRunning];

【问题讨论】:

  • 在 if / else 语句周围使用方括号。省略它们可以有really bad consequences
  • @mattt 我添加了它们,但没有任何改变,但仍然没有相机的实时视图
  • 这是一个笼统的说法,并不是专门针对这个问题进行调试的。

标签: ios view camera ios8 xcode6


【解决方案1】:

试试这个...

在--viewWillAppear中:在主线程开始摄像头捕捉,像这样..

dispatch_async(dispatch_get_main_queue(), ^{

if (![session isRunning])
    {              
            [session startRunning];
    }
});

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 2014-11-07
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2015-11-11
    • 2015-06-30
    • 1970-01-01
    相关资源
    最近更新 更多