我想为我的应用做一个实时相机预览,但是当我在我的设备上运行应用时,它会崩溃。我写了一些代码,实际上,我认为,这些代码必须有效。
@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.'
答案 0 :(得分:0)
您无法在UIView上捕获视频预览,您需要UIImageView来呈现视频捕获预览。
将vImagePreview类型更改为UIImageView,如下所示
@property(nonatomic, retain) IBOutlet UIImageView *vImagePreview;