Ios `[AVCaptureSession CanadOutput:output]`间歇返回NO。我能找出原因吗?
我正在使用Ios `[AVCaptureSession CanadOutput:output]`间歇返回NO。我能找出原因吗?,ios,objective-c,avcapturesession,avcapturemoviefileoutput,Ios,Objective C,Avcapturesession,Avcapturemoviefileoutput,我正在使用canAddOutput:来确定是否可以将AVCaptureMovieFileOutput添加到AVCaptureSession中,我发现canAddOutput:有时返回NO,大部分返回YES。有没有办法找出拒绝被退回的原因?或者是一种消除导致返回NO的情况的方法?或者我能做些什么来防止用户看到间歇性故障 进一步说明:这种情况大约每30次通话中发生一次。由于我的应用程序尚未启动,因此只在一台设备上进行了测试:运行7.1.2的iPhone 5可能是以下几种设备的组合: 当相机忙时调用
canAddOutput:
来确定是否可以将AVCaptureMovieFileOutput
添加到AVCaptureSession
中,我发现canAddOutput:
有时返回NO,大部分返回YES。有没有办法找出拒绝被退回的原因?或者是一种消除导致返回NO的情况的方法?或者我能做些什么来防止用户看到间歇性故障
进一步说明:这种情况大约每30次通话中发生一次。由于我的应用程序尚未启动,因此只在一台设备上进行了测试:运行7.1.2的iPhone 5可能是以下几种设备的组合:
- 当相机忙时调用此方法
- 未正确删除以前连接的
AVCaptureSession
canAddOutput:
将始终是YES
),然后根据需要暂停/恢复会话:
// Stop session if possible
if (_captureSession.running && !_captureInProgress)
{
[_captureSession stopRunning];
NBULogVerbose(@"Capture session: {\n%@} stopped running", _captureSession);
}
您可以看一看。参考,可能是在后台运行,这会导致前一个AVCaptureSession
未正确断开连接,有时会导致canAddOutput:
变为NO
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
解决方案可能是在上述委托中使用stopRunning
(当然,在执行必要的操作和条件检查之后,您需要正确地完成以前的会话,对吗?)
除此之外,如果您提供一些您正在尝试执行的操作的代码,则会更好。我认为这将对您有所帮助
加拿大产量:
返回一个布尔值,该值指示是否可以将给定的输出添加到会话
- (BOOL)canAddOutput:(AVCaptureOutput *)output
参数
输出
要添加到会话的输出。
返回值
如果可以将输出添加到会话,则为“是”,否则为“否”
可用性
在OS X v10.7及更高版本中提供
这里是apple文档的链接这里引用了文档中的内容(讨论了
CanadOutput:
)
添加捕获输入:要将捕获设备添加到捕获会话,请使用AVCaptureDeviceInput实例(具体的 抽象AVCaptureInput类的子类)。捕获设备输入管理设备的端口
NSError * error = nil;
AVCaptureDeviceInput * input =
[AVCaptureDeviceInput deviceInputWithDevice: device error: & error];
if (input) {
// Handle the error appropriately.
}
添加输出,输出分类:
要从捕获会话获取输出,请添加一个或多个输出。输出是一个具体实例
AVCaptureOutput的子类您使用:
AVCaptureMovieFileOutput输出到电影文件
AVCaptureVideoDataOutput,如果要处理所捕获视频中的帧
AVCaptureAudioDataOutput,如果要处理正在捕获的音频数据
AVCaptureStillImageOutput,如果要捕获带有元数据的静态图像 您可以使用addOutput:将输出添加到捕获会话。
检查捕获输出是否兼容 使用
canAddOutput:
您可以根据需要添加和删除输出,而 会话正在运行
AVCaptureSession * captureSession = <# Get a capture session #>;
AVCaptureMovieFileOutput * movieInput = <# Create and configure a movie output #>;
if ([captureSession canAddOutput: movieInput]) {
[captureSession addOutput: movieInput];
}
else {
// Handle the failure.
}
能够支持不同格式,也支持直接生成jpg流。
如果要捕获JPEG图像,通常不应指定自己的压缩格式。相反
您应该让静态图像输出为您进行压缩,因为它的压缩是硬件加速的。
如果需要图像的数据表示形式,可以使用jpegStillImageNSDataRepresentation:来
获取NSData对象而无需重新压缩数据,即使您修改了映像的元数据也是如此
摄像头预览显示:
AVCaptureStillImageOutput * stillImageOutput = [[AVCaptureStillImageOutput alloc]
init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG,
AVVideoCodecKey, nil];
[StillImageOutput setOutputSettings: outputSettings];
您可以使用AVCaptureVideoPreviewLayer为用户提供录制内容的预览
对象AVCaptureVideoPreviewLayer是CALayer的一个子类(请参阅核心动画编程指南)。显示预览不需要任何输出
AVCaptureSession * captureSession = <# Get a capture session #>;
CALayer * viewLayer = <# Get a layer from the view in which you want to present the
The preview #>;
AVCaptureVideoPreviewLayer * captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer
alloc] initWithSession: captureSession];
[viewLayer addSublayer: captureVideoPreviewLayer];
AVCaptureSession*captureSession=;
CALayer*视图层=;
AVCaptureVideoPreviewLayer*captureVideoPreviewLayer=[[AVCaptureVideoPreviewLayer]
alloc]initWithSession:captureSession];
[查看层添加子层:captureVideoPreviewLayer];
通常,预览层的行为类似于渲染树中的任何其他CALayer对象(请参见核心动画)
编程指南)。您可以根据需要缩放图像并执行变换、旋转等操作
一个不同之处是,您可能需要设置图层的方向属性来指定方向
它应该旋转来自相机的图像。此外,在iPhone4上,预览层支持镜像
(这是预览前置摄像头时的默认设置)。它可以是这两种情况中的一种
1) 会话正在运行
2) 您已经添加了输出
你不能添加两个输出或两个输入,也不能创建两个不同的会话你能给我看一下你要做的事情的代码吗?
AVCaptureMovieFileOutput * aMovieFileOutput = [[AVCaptureMovieFileOutput alloc]
init];
CMTime maxDuration = <# Create a CMTime to represent the maximum duration #>;
aMovieFileOutput.maxRecordedDuration = maxDuration;
aMovieFileOutput.minFreeDiskSpaceLimit = <# An appropriate minimum given the quality
of the movie format and the duration #>;
AVCaptureStillImageOutput * stillImageOutput = [[AVCaptureStillImageOutput alloc]
init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG,
AVVideoCodecKey, nil];
[StillImageOutput setOutputSettings: outputSettings];
AVCaptureSession * captureSession = <# Get a capture session #>;
CALayer * viewLayer = <# Get a layer from the view in which you want to present the
The preview #>;
AVCaptureVideoPreviewLayer * captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer
alloc] initWithSession: captureSession];
[viewLayer addSublayer: captureVideoPreviewLayer];