Domanda

I want to make a live camera preview for my app, but it's crashing when I'm running the app on my device. I've written some code, which actually, i think, has to work.

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

I made an UIView and I linked it to vImagePreview, but it is still not working... It is giving a crash:

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.'
È stato utile?

Soluzione

You cannot capture video preview on UIView, you need UIImageView to render your video capture preview.

Change vImagePreview type to UIImageView as follows

@property(nonatomic, retain) IBOutlet UIImageView *vImagePreview;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top