gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:29 25 4
gpt4 key购买 nike

我正在使用以下代码将相机嵌入到我的应用程序 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com