【发布时间】:2012-12-13 16:04:41
【问题描述】:
我正在 iOS 上构建一个原型应用程序,并且我正在蚕食一些 Apple 示例代码来完成它(我知道,这很简单——这段代码使用 goto 语句:\)。我正在使用 Session 520 - What's New in Camera Capture 中的 AVCam 项目。我不需要视频捕捉功能,只需要静态照片。
设备的输入和输出是这样设置的:
// Init the device inputs
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil];
AVCaptureDeviceInput *newAudioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioDevice] error:nil];
// Setup the still image file output
AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};
[newStillImageOutput setOutputSettings:outputSettings];
// Create session (use default AVCaptureSessionPresetHigh)
AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
// Add inputs and output to the capture session
if ([newCaptureSession canAddInput:newVideoInput]) {
[newCaptureSession addInput:newVideoInput];
}
if ([newCaptureSession canAddInput:newAudioInput]) {
[newCaptureSession addInput:newAudioInput];
}
if ([newCaptureSession canAddOutput:newStillImageOutput]) {
[newCaptureSession addOutput:newStillImageOutput];
}
[self setStillImageOutput:newStillImageOutput];
[self setVideoInput:newVideoInput];
[self setAudioInput:newAudioInput];
[self setSession:newCaptureSession];
这是当我点击快门按钮时调用的方法:
- (void) captureStillImage
{
AVCaptureConnection *stillImageConnection = [[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo];
if ([stillImageConnection isVideoOrientationSupported])
[stillImageConnection setVideoOrientation:orientation];
[[self stillImageOutput]
captureStillImageAsynchronouslyFromConnection:stillImageConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
if (error)
{
if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)])
{
[[self delegate] captureManager:self didFailWithError:error];
}
}
};
if (imageDataSampleBuffer != NULL)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image = [[UIImage alloc] initWithData:imageData];
if ([self.delegate respondsToSelector:@selector(captureManagerCapturedImage:)])
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate captureManagerCapturedImage:image];
});
}
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:completionBlock];
}
else
{
completionBlock(nil, error);
}
if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)])
{
[[self delegate] captureManagerStillImageCaptured:self];
}
}];
}
此代码成功捕获图像并将其保存到库中。然而,在我研究它的某个时候,它从捕捉 5 兆像素 4:3 图像变为捕捉 1920x1080 16:9 图像。我找不到任何指定纵横比的地方,也没有更改与相机配置、捕获会话或捕获连接相关的任何代码。为什么我的相机开始拍摄 16:9 的照片?
更新:我刚刚重新运行了 Apple 的原始示例代码,它似乎也在保存直接从视频中捕获的 16:9 图像。很可能我之前发疯了,或者我用 Camera.app 进行了测试并正在查看它。所以我真正的问题是,我如何在拍摄时在屏幕上显示来自相机的实时信息,并拍摄全分辨率照片。我不能使用UIImagePickerController,因为我需要能够在实时摄像头提要上叠加内容。
更新 2:我能够通过丢弃我正在使用的 AVCapture 代码来解决这个问题。事实证明 UIImagePickerController 做了我需要的。我没有意识到你可以覆盖自定义控件 - 我认为它会占据整个屏幕,直到你完成拍照。
【问题讨论】:
-
那么你从相机捕捉帧?因为这意味着你以 16:9 结束。捕捉帧和拍照是不同的事情。
-
啊,这可能是问题所在。我想捕捉一张全分辨率的照片,而不仅仅是视频中的一帧。奇怪的是,据我所知,我一直在拍摄全分辨率照片,直到昨天的某个时候,它似乎自发地改变了。
-
如果这可以解决您的问题,我会将我的评论复制到答案中。如果没有,我们必须深入研究代码。
-
更新了问题以反映我不知道自己在做什么的事实:-P
标签: objective-c ios ipad cocoa-touch