【问题标题】:Camera Live Preview doesn't work, SIGABRT相机实时预览不起作用,SIGABRT
【发布时间】:2014-01-10 05:16:21
【问题描述】:

我想为我的应用制作实时相机预览,但是当我在我的设备上运行该应用时它崩溃了。我写了一些代码,我认为它实际上必须工作。

@synthesize vImagePreview;

-(void)viewDidLoad {

    [super viewDidLoad];

}

- (void)viewDidUnLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        vImagePreview = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];



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

    CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

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

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

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    [session startRunning];
}

我制作了一个 UIView 并将其链接到 vImagePreview,但它仍然无法正常工作... 它正在崩溃:

2013-12-21 13:22:09.086 UnknownApp[1862:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x13f500230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key vImagePreview.'

【问题讨论】:

  • 您是否在界面文件中指定了 vImagePreview 属性?
  • 你能告诉我你是怎么申报的吗?
  • @ interface ViewController : UIViewController { IBOutlet UIView *vImagePreview; @property(nonatomic, 保留) IBOutlet UIView *vImagePreview; @end
  • 我已经发了答案,请参考!!!

标签: objective-c avcapturesession


【解决方案1】:

你不能在 UIView 上捕捉视频预览,你需要 UIImageView 来渲染你的视频捕捉预览。

如下改变vImagePreview类型为UIImageView

@property(nonatomic, retain) IBOutlet UIImageView *vImagePreview;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多